我对 OOP 真的很陌生,我在第一次实现 __constructor 方法时遇到了一些错误。这两个错误是:
Notice: Undefined variable: _test in...
Fatal error: Cannot access empty property in ...
这是我的代码:
<?php
class Info
{
private static $_test;
public function __construct()
{
$this->setIt(); //call setIt so $_test can be set to true or false for use by other functions in the class.
}
public function setIt()
{
if ("test" == "something") //simplified logic for demo purposes
$this->$_test = TRUE;
else
$this->$_test = FALSE;
}
public function getIt()
{
if ($this->$_test) { //if test is true or false do stuff/other stuff
//do stuff
} else {
//do other stuff
}
}
}
$myObj = new Info(); //initialise the object
?>
<?php echo $myObj->getIt(); ?> //surrounded by html in my original code. Here I am getting some 'stuff' or 'other stuff' depending on what setIt() returned to $_test.
对不起,如果这有点令人费解。