如何从类A中实例化的对象访问类A的属性。
class A()
public var1;
public obj1;
function __construct(){
$this->var1 = 'Hello World';
$this->obj1 = new B();
}
==============
class B()
function anything(){
#i want to access var1 from the calling class here ????
# how do i access var1 in the calling class
}
答案 0 :(得分:3)
没有直接的方法可以做到这一点。依赖注入是一种可能性:
class B {
protected $A = null;
public function __construct($A) {
$this->A = $A;
}
public function foo() {
$this->A->var1;
}
}
class A {
public function __construct() {
$this->obj1 = new B($this);
}
}