私はOOP の初心者で、__constructor メソッドの最初の実装でいくつかのエラーが発生しています。2 つのエラーは次のとおりです。
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.
少し複雑な場合は申し訳ありません。