0
class GrandClass {
    public $data;
    public function __construct() {
        $this->someMethodInTheParentClass();
    }
    public function someMethodInTheParentClass() {
        $this->$data = 123456;
    }
}

class MyParent extends GrandClass{
    public function __construct() {
        parent::__construct();
    }
}

class Child extends MyParent {
    // public $data;
    public function __construct() {
        parent::__construct();
    }
    public function getData() {
        return $this->data; 
    }
}

$a = new Child();
var_dump($a->getData());

PHP Notice: Undefined variable: data in D:\test.php on line 7

PHP Fatal error: Cannot access empty property in D:\test.php on line 7

전창한
  • 21
  • 2

3 Answers3

4

update your function someMethodInTheParentClass with below using $this->data = 123456;

 public function someMethodInTheParentClass() {
        $this->data = 123456;
    }
AmmyTech
  • 738
  • 4
  • 10
2
Use  `$this->data = 123456; `instead of`  $this->$data = 123456;` in below function

public function someMethodInTheParentClass() {
        $this->data = 123456;
}
Kamlesh Gupta
  • 505
  • 3
  • 17
  • You might wanna flesh out your answer a little if you can, and perhaps format your answer a bit :) – Epodax Aug 24 '16 at 06:43
0

Constructors in MyParent and Child classes are unnecessary.

Vuer
  • 149
  • 6