有没有办法从声明类中的素数类中获取变量?
问题描述:
for example -
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
Is there a way of doing this?
why the f*** always people minusing my questions? What is your problem? This site is for asking questions, if you have a problem so don't come to this site.
答
Adding on from my comment.. try this
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub extends wow
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
答
You should extend the parent class
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub extends wow
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
This will return 5.
And you have other errors in your code
class sub()
Should be
class sub
And
public __construct()
Should be
public function __construct()