How do I get class name in PHP?

后端 未结 10 1887
名媛妹妹
名媛妹妹 2020-11-29 04:37
public class MyClass {

}

In Java, we can get class name with String className = MyClass.class.getSimpleName();

How to do this

相关标签:
10条回答
  • 2020-11-29 04:39

    You can use __CLASS__ within a class to get the name.

    http://php.net/manual/en/language.constants.predefined.php

    0 讨论(0)
  • 2020-11-29 04:43
    echo substr(strrchr(__CLASS__, "\\"), 1);    
    
    0 讨论(0)
  • 2020-11-29 04:45

    To get class name you can use ReflectionClass

    class MyClass {
        public function myNameIs(){
            return (new \ReflectionClass($this))->getShortName();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 04:48

    Since PHP 5.5 you can use class name resolution via ClassName::class.

    See new features of PHP5.5.

    <?php
    
    namespace Name\Space;
    
    class ClassName {}
    
    echo ClassName::class;
    
    ?>
    

    If you want to use this feature in your class method use static::class:

    <?php
    
    namespace Name\Space;
    
    class ClassName {
       /**
        * @return string
        */
       public function getNameOfClass()
       {
          return static::class;
       }
    }
    
    $obj = new ClassName();
    echo $obj->getNameOfClass();
    
    ?>
    

    For older versions of PHP, you can use get_class().

    0 讨论(0)
  • 2020-11-29 04:53

    It sounds like you answered your own question. get_class will get you the class name. It is procedural and maybe that is what is causing the confusion. Take a look at the php documentation for get_class

    Here is their example:

     <?php
    
     class foo 
     {
         function name()
         {
             echo "My name is " , get_class($this) , "\n";
         }
     }
    
     // create an object
     $bar = new foo();
    
     // external call
     echo "Its name is " , get_class($bar) , "\n"; // It's name is foo
    
     // internal call
     $bar->name(); // My name is foo
    

    To make it more like your example you could do something like:

     <?php
    
     class MyClass
     {
           public static function getClass()
           {
                return get_class();
           }
     }
    

    Now you can do:

     $className = MyClass::getClass();
    

    This is somewhat limited, however, because if my class is extended it will still return 'MyClass'. We can use get_called_class instead, which relies on Late Static Binding, a relatively new feature, and requires PHP >= 5.3.

    <?php
    
    class MyClass
    {
        public static function getClass()
        {
            return get_called_class();
        }
    
        public static function getDefiningClass()
        {
            return get_class();
        }
    }
    
    class MyExtendedClass extends MyClass {}
    
    $className = MyClass::getClass(); // 'MyClass'
    $className = MyExtendedClass::getClass(); // 'MyExtendedClass'
    $className = MyExtendedClass::getDefiningClass(); // 'MyClass'
    
    0 讨论(0)
  • 2020-11-29 04:56
    <?php
    namespace CMS;
    class Model {
      const _class = __CLASS__;
    }
    
    echo Model::_class; // will return 'CMS\Model'
    

    for older than PHP 5.5

    0 讨论(0)
提交回复
热议问题