Domanda

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

È stato utile?

Soluzione

<?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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top