Get class name in static method
| From: | Stut | Date: | Mon, 20 Mar 2006 14:29:14 +0000 |
| Subject: | Get class name in static method | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-232235@lists.php.net to get a copy of this message | ||
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 it's not possible and I just wanna check to make sure before I admit defeat.
Say I have the following...
class Foo
{
public static function & Create()
{
$classname = ???;
return new $classname;
}
}
class Bar extends Foo { }
$obj = Bar::Create();
What do I put where the ???s are? I've tried __CLASS__ and get_class() but both give me the base class name. Short of passing the classname to the static function I'm at a loss as to how to do this. However, Bar::Create('Bar') is a bit too sucky for me to be happy with it and kinda defeats the object.
Thanks.
-Stut