我想知道为什么下面的代码不会打印出任何东西。我试图从父类中的方法访问Bar::$some_var
。其中Bar::$some_var
在其构造函数中定义。试图了解PHP OOP
我试过self::$some_var
和static::$some_var
在Foo::hello()
但都没有工作。我必须做$some_var
static
?
class Foo {
private $some_var;
public function __construct() {
$this->some_var = 5;
}
public function hello() {
print $this->some_var;
}
}
class Bar extends Foo {
public function __construct() {
$this->some_var = 10;
}
}
$bar = new Bar();
$bar->hello();
在此先感谢。
这应该有助于http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php – elclanrs
仅供参考,静态结构是不是面向对象的一部分范例。另外,这个问题与OOP无关。这是关于学习语言的语法。 –