我想要一个具有基本属性和函数的基类,所以我不必在所有子类中定义它们。
我使用php 5.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`."
可能的重复:http://stackoverflow.com/questions/190421/caller-function-in-php-5 – 2010-12-22 16:27:45