Php static function get class name

2019-08-21 16:52

A protip by markusperl about php, php class name without namespace, php class name, and php class namespace.php at itronic dot at. 9 years ago. If you call a static getInstance() function to create a instance of a class from another class, this function have to be static, if it is not static the original name of the caller class and not of the current class get returned. example: php static function get class name

Although you can call a class's static methods from an instance of the class as though they were object instance methods, it's nice to know that, since classes are represented in PHP code by their names as strings, the same thing goes for the return value of getclass():

Static Keyword. Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can). For compatibility with PHP 4, if no visibility declaration is used, In case you don't want to use getcalledclass() you can use other tricks of late static binding (PHP 5. 3). But the downside in this case you need to have getClass() method in every model.php static function get class name I'm fairly certain the first one is faster since it avoids parsing the string to retrieve the class name and method name. But that's just a guess, maybe the array initialization takes more time than parsing the string. jurchiks Nov 22 '15 at 16: 48

(3 replies) Hi, does anyone know a way of how to get the name of the class within a static function? I can't use CLASS as my PHP version is and I can't upgrade. [PHP How to get class name in static function (PHP ) Torsten Roehr. May 16, 2004 at 5: 43 pm: Hi, does anyone know a way of how to get the name of the class within a php static function get class name [PHP Get class name in static method; Stut. Mar 20, 2006 at 2: 29 pm: Hi all, I'm wanting to define a static method on a base class that then instantiates and returns an object of the type the static method was called on. I've searched the docs and Googled but everything I find says I get the feeling that there is some confusion regarding dynamic typing in PHP: There is no such thing as an object of type class in PHP, classes are expressed by their names. So if you write foo in your application, you will get a WARNING and that foo will be interpreted as the string 'foo' even if there is a class by that name. . So if you need to call a static function on a class that is If you want to use this feature in your class method use static: : class: For older versions of PHP, you can use getclass(). You can use CLASS within a class to get the name. It sounds like you answered your own question. getclass will get you the class name. It is procedural and maybe that is what is causing the confusion.

Rating: 4.52 / Views: 335

Php static function get class name free