Reputation: 101
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
Upvotes: 5
Views: 6287
Reputation: 7054
If really wanting to avoid the statics I think the Reflection class might work.
function getClassName(ReflectionParameter $param) {
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
That's from the comments on http://www.php.net/manual/en/reflectionparameter.getclass.php
Upvotes: 0
Reputation: 16709
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
Upvotes: 7
Reputation: 6909
echo get_class($this);
should work inside of a class.
echo __CLASS__;
I believe this is a static property
Upvotes: 2