Question

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 ?

Was it helpful?

Solution

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:

If you are refering to Late Static binding, have a look at


I 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() );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top