Thornig
Thornig

Reputation: 13

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!

Upvotes: 1

Views: 86

Answers (1)

MrCode
MrCode

Reputation: 64526

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.

Upvotes: 2

Related Questions