Search code examples
phpstatic-methodsdynamic-binding

Dynamic binding in static method php


class A
{
  static function get_name_derived_class()
  {
      //This function must return the name of the real class
      //Is it possible without insert a methon in B class?
  {
}

class B extends A
{

}

B::test()

I'd like to have a static methon in base class which returns the name of real (derived) class, without insert a specific method in it. is it possible? thanx


Solution

  • <?php
    
    class A
    {
        static function test()
        {
            return get_called_class();
        }
    }
    
    class B extends A
    {
    }
    
    echo B::test();
    

    Requires PHP >= 5.3.0. See PHP's manual entry on Late Static Bindings