我是新来的OOP模式,所以可能存在该问题的一个简单的解释...
你总是需要一个类来声明社会的广泛对象的变量? 例如:
<?php
class TestClass
{
var $declaredVar;
function __construct()
{
$this->declaredVar = "I am a declared variable.";
$this->undeclaredVar = "I wasn't declared, but I still work.";
}
function display()
{
echo $this->declaredVar . "<br />";
echo $this->undeclaredVar;
echo "<br /><br />";
}
}
$test = new TestClass;
$test->display();
$test->declaredVar = "The declared variable was changed.";
$test->undeclaredVar = "The undeclared variable was changed.";
$test->display();
?>
在这段代码中,即使$declaredVar
是唯一声明的变量, $undeclaredVar
是一样访问和使用-它似乎表现得好像我已经宣布它为公众。
如果未声明的类变量总是这样访问,什么是他们声明所有前面的点?