PHP member data lost when abstract method returns

FeKuLa

New Member
Here's an example of an abstract class and a derived class. There is one abstract method, "collectData" in class A, that is implemented in class B. When the method "getData" is called, "collectData" is then called, setting values in the private member variable "$data". Afterward, getData returns the contents of this variable. If you run this, you would expect the return value to be array(1, 2, 3). But it is an empty array. Why? I'm using PHP 5.3.10 by the way.\[code\]<?phpabstract class A{ private $data; public function __construct() { $this->data = http://stackoverflow.com/questions/10547990/array(); } abstract protected function collectData(); public function getData() { $this->collectData(); return $this->data; }}class B extends A{ protected function collectData() { $this->data = array(1, 2, 3); }}$test = new B();$data = $test->getData();print_r($data);\[/code\]
 
Back
Top