It's possible to do this if the class B NOT extended the class A but the class A call a new class B
class A{ public $lang; public function __construct($lang) { $this->lang=$lang; } public function new_B(){ return new B(); } } class B{ public function __construct() { echo 'lang='.A::$lang; } } $root=new A('eng'); $root->new_B();
You seem to have a mixup of concepts here. The $lang property of A is an instance level variable (since it is not defined as static), therefore you cannot access it statically as you are trying to. If you were to declare the variable as static then you would have access to it, but if you have multiple instances of class A that change it, it will change on the class level, rather than instance level.
change the class B like this:
class B extends A{
public function __construct() {
echo 'lang='.$this->$lang; // you can use parent variables like this
}
}
Is A::$lang common to all A objects you will create? Then make this variable static. If not you can pass A::$lang as parameter to the B constructor. That is
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B($this->lang);
}
}
class B{
public function __construct($lang) {
echo 'lang='.$lang;
}
}
Following is making A::$lang static:
class A{
public static $lang;
public function __construct($lang) {
self::$lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}