Honzo
Honzo

Reputation: 103

Finding a class name after extending

I'm having troubles finding a class name after extending other classes. Here are the relevant classes:

class Home extends Controller {
       public function test() {
           Example::all();
       }
}


Class Example extends Active {
    //Variables
}

Class Active extends Database {
     public function all() {
        //This is where I need to store a variable containing the class name Example.
     }
}

I'm trying to retrieve the name of the Class Example in Class Active from when it is called from class Home, however I've found it impossible to do this so far without adding an extra argument (which I don't want to do).

Upvotes: 0

Views: 117

Answers (2)

Gordon
Gordon

Reputation: 316969

You are looking for

Example (demo)

class Home  {
       public function test() {
           Example::all();
       }
}
Class Example extends Active {
    //Variables
}
Class Active  {
     public static function all() {
        var_dump( get_called_class() );
     }
}

Note that I have changed the signature of Active::all to static because calling non-static methods statically will raise E_STRICT warnings. In general, you want to avoid static methods and Late Static Binding.

Upvotes: 2

wezzy
wezzy

Reputation: 5935

have you tried with ReflectionObject class from PHP ? Have a look to this method

Hope this helps

Upvotes: 0

Related Questions