5

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
7
  • $obj->tempFn(new MyClass()) or $myClass = new MyClass(); //do something with this object $obj->tempFn($myClass); Commented Feb 20, 2013 at 16:33
  • Note that new MyClass() creates an instance of the class MyClass, its not the same as a reference to a class. But why would you need to do this anyway? Commented Feb 20, 2013 at 16:34
  • Please explain what you mean by "contextually be represented as a string". What are you attempting to do? The question can interpreted in a few ways. Commented Feb 20, 2013 at 16:36
  • I'd like to do it this way because it would be a clean, direct reference to the class instead of a string (where human error can cause issue and the IDE can't detect that it's a reference to an actual class). I want the result to be a string but without directly typing a string. Hopefully that makes sense! Commented Feb 20, 2013 at 16:41
  • 2
    @Sverri: OP is trying to get the fully qualified class name. This is useful when you use namespaces and you want to pass the class name to a function or something. When using strings, you're forced to prepend the namespace too :( Commented Feb 20, 2013 at 16:45

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

Comments

2

echo get_class($this);should work inside of a class.

echo __CLASS__; I believe this is a static property

6 Comments

Thank you, but I need to refer to it outside of the class itself and without instantiating the class first.
You can return the __CLASS__ property inside a static method (i.e. static function getName())?
@astonius is this a joke? If you dont instantiate the class, you can access it only via static methods and variables. To access static things you must know the name of the class. Like Math::sum(). Just write manually echo "Math"??
@Husman Math::getName() returns Math. The same as Husman! What's your name? -Husman... :DDD
@Jari Yep, it does, you have your thinking hat on today, but that was what was asked for. "I need to refer to it outside of the class itself and without instantiating the class first"
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.