Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP class name as a string

Tags:

php

class

Is there a built-in static method or property to refer to a PHP class so that it will contextually be represented as a string? For example:

Instead of this:

$obj->tempFn('MyClass') //MyClass being the name of the class

I want to do this:

$obj->tempFn(MyClass) //Directly references the class name, instead of a string representation
like image 884
astonius Avatar asked Feb 20 '13 16:02

astonius


1 Answers

No. But you can define a constant in your class, that contains the class name, like:

class foo{
  const  
    NAME = 'foo';
}

And access it like foo::NAME.

In PHP 5.5, you'll be able to use:

foo::class
like image 170
nice ass Avatar answered Sep 16 '22 19:09

nice ass