-1

私は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.

少し複雑な場合は申し訳ありません。

4

3 に答える 3

1

交換

$this->$_test = TRUE;

$this->_test = TRUE;

コードのどこにでも。

$thisthenを使用$している間、プロパティ名では使用されません

このスレッドは役に立ちます。

于 2013-05-28T11:17:19.477 に答える