简体   繁体   中英

PHP Class Extends - Parent var in Child

This code do not run. I should echo parent var, in my child class. Please help. Thanks!

class A {
    public $valtozo;
    function show ($num) {
        $this->valtozo = $num;
    }
}

class B extends A {

    function mas () {
        echo parent::$valtozo;
    }
}

$oszatly = new B();
$oszatly->show(55);
$oszatly->mas();

Error:

Fatal error: Access to undeclared static property: A::$valtozo in C:\\AppServ\\www\\testi.php on line 13

Thans mans!

Your property is not static, so you should not use the :: syntax to access it. Instead use $this-> . Change to:

function mas () {
    echo $this->valtozo;
}

By using $this-> , you can access properties and methods from the parent class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM