Domanda

I would like to display a parent Class variable, I can't find a way to solve the situtation...

Here is my PHP :

class A {

    public $a;

}

class B extends A {

    public function __construct() {
        echo $parent->a;
    }

}

$B = new B();

This is supposed to output $a, in my case $a is an PDO object, and instead of print it, i call a prepare() on it :)

like that :

class A {

    public $a;

}

class B extends A {

    public function __construct() {
        $this->a->prepare('random SQL request');
    }

}

$B = new B();

I have a "Cannot access empty property" PHP error

Thanks !

È stato utile?

Soluzione 2

Something like this would work:

class A {

    public $a = "Hello World";

}

class B extends A {

    public function __construct() {
        echo $this->a;
    }

}

$B = new B();

Run it:

php parent.php

Hello World

Altri suggerimenti

echo $this->a;

Many times the comments in the PHP manual are as valuable as the manual itself: http://www.php.net/manual/en/keyword.parent.php#42153

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top