Php 获取父类中子类的函数名

Php 获取父类中子类的函数名,php,oop,php-5.3,Php,Oop,Php 5.3,我希望有一个具有基本属性和函数的基类,因此我不必在所有子类中定义它们。 我使用PHP5.3.3 这不可能吗 class A { private $debug; private $var; protected function setVar($str) { $this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.'; $this->var =

我希望有一个具有基本属性和函数的基类,因此我不必在所有子类中定义它们。
我使用PHP5.3.3

这不可能吗

class A {
  private $debug;
  private $var;
  protected function setVar($str) {
    $this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.';
    $this->var = $str;
    return true;
  }
  protected function getVar() {
    return $this->var;
  }
  protected function getDebug() {
    return $this->debug;
  }
}
class B extends A {
  public function __construct() {
    $this->doSomething();
  }
  public function doSomething() {
    $this->setVar('my string');
  }
}
$myobj = new B();
$myobj->getDebug();
// expected output "Set by function `doSomething` in class `B`."

查看函数。注意:此功能非常昂贵,因此您应该在生产中禁用这些调试功能。

这对您没有好处吗

我没有在本地运行5.3,所以我不得不将get_转换为_class(),但您仍然可以使用它。应该说清楚的,对不起

class A {
  private $debug;
  private $var;
  protected function setVar($str, $class) {
    $this->debug = 'Set by function `` in class `'. $class .'`.';
    $this->var = $str;
    return true;
  }
  protected function getVar() {
    return $this->var;
  }
  public function getDebug() {
    return $this->debug;
  }
}
class B extends A {
  public function __construct() {
    $this->doSomething();
  }
  public function doSomething() {
    $this->setVar('my string', __CLASS__);
  }
}
$myobj = new B();
echo $myobj->getDebug();
可能重复:
class A {
  private $debug;
  private $var;
  protected function setVar($str, $class) {
    $this->debug = 'Set by function `` in class `'. $class .'`.';
    $this->var = $str;
    return true;
  }
  protected function getVar() {
    return $this->var;
  }
  public function getDebug() {
    return $this->debug;
  }
}
class B extends A {
  public function __construct() {
    $this->doSomething();
  }
  public function doSomething() {
    $this->setVar('my string', __CLASS__);
  }
}
$myobj = new B();
echo $myobj->getDebug();