在我的代码中,我有两个类 - 第一个“Foo”启动第二个“Bar”...我想要做的是找到使用父函数和变量的一些方法。通过类递归进行函数调用?
class Bar {
function __construct() {
/*
* From within this function, how do I access either the returnName() function
* OR the $this -> name variable from the class above this?
* The result being:
*/
$this -> name = parent::returnName();
$this -> else = parent -> name;
}
}
class Foo {
function __construct() {
$this -> name = 'Fred';
$this -> var = new Bar();
}
function returnName() {
return $this -> name;
}
}
$salad = new Foo();
我意识到“父”的语法是指一个实现或扩展,但是有可能使用这种方法吗?
谢谢,我知道我会做这个 - 虽然我没有想通过作为一个论据,除非我绝对必须。 – lsrwLuke
[debug_backtrace()](http://php.net/manual/en/function.debug-backtrace.php)查看我的答案,查看您的课程示例; –
对于Daan,Bar类现在是试图使用来自Foo的函数,“调用未定义的方法Bar :: returnName()”。解决这个问题? – lsrwLuke