Reputation: 12904
Is there any way to get the Derived Class Name from static Keyword ? What I actually want to do is.
Make a Reflection of the Derived Class
Check Wheather it implements the Specified Class or not
I've a static method the Base Class say its named Base::check()
I want Base::check()
will check for the existence of the Interface Implementation.
the check() method knows the name of the interface.so I don't need to tell it from outer world.
But if there is no way to get DerivedClassName from static Keyword I need to pass the Class Name to the method from the outer world. Which is not a good Coding Principle.
Another Alternative I can do is. I can make an ::instance()
static method that returns the this
and I can do a get_class from its return. But this also tweeks my Application Principle.
So is the first method really possible ? or I need to go for some alternative of this kind ? Or Do you have any other kind of Alternatives ?
Upvotes: 0
Views: 107
Reputation: 317217
I am not sure if I understand what you are trying to do, especially what you mean by "get the Derived Class Name from static Keyword". Check out the following functions to see if they do what you want to do:
class_implements
— Return the interfaces which are implemented by the given class class_parents
— Return the parent classes of the given class is_a
— Checks if the object is of this class or has this class as one of its parentsis_subclass_of
— Checks if the object has this class as one of its parentsinstanceof
Type OperatorIf you are refering to Late Static binding, have a look at
get_called_class
— the "Late Static Binding" class nameI suppose you are trying to do something like this:
class Base
{
public static function check()
{
return in_array('IFoo', class_implements(get_called_class(), FALSE));
// or
$instanceClassName = get_called_class();
return new $instanceClassName instanceof IFoo;
// or
$reflector = new ReflectionClass(get_called_class());
return $reflector->implementsInterface('IFoo');
}
}
interface IFoo {};
class Foo extends Base implements IFoo {}
var_dump( Foo::check() );
Upvotes: 2