开发者

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).


You are looking for

  • get_called_class — the "Late Static Binding" class name

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.


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

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜