I am getting fatal error while running the following code. Any solutions to avoid the error.
The error is
PHP Fatal error: Call to a member function getD() on a non-object in ..... on line 24.
The objective is to call the getValue() function of of D type object. Your guidance is required. I am using PHP5+ and Apache 2.2 on windows XP.
The code is below:
Expand|Select|Wrap|Line Numbers
- <?php
- class Z{
- function Z(){
- }
- }
- class A extends Z{
- function A(){
- parent::Z();
- }
- function &getB(){
- $b = new B();
- return $b;
- }
- }
- class B{
- private $c;
- public function B(){
- $this->c = new C();
- }
- public function &getD(){
- return $this->c->getD();
- }
- public function &getE(){
- return $this->c->getE();
- }
- }
- class C{
- private $f;
- public function C(){
- $this->f = array();
- }
- private function setD(){
- $this->f[0][0] = new D();
- }
- private function setE(){
- $this->f[0][1] = new E();
- }
- public function &getD(){
- return $this->f[0][0];
- }
- public function &getE(){
- return $this->f[0][1];
- }
- }
- class D{
- public function D(){
- }
- public function getValue(){
- return "Val";
- }
- }
- class E{
- public function E(){
- }
- public function getValue(){
- return "Eval";
- }
- }
- $a = new A();
- $b =& $a->getB();
- $d = $b->getD();
- echo $d->getValue()
- ?>