Q: Why the following code produces memory exhausted error.? I am using the following code and if i run it it produces memory exhausted error.
class A{
  public function who(){
   echo __CLASS__;
 }
 public function test(){
   echo $this->who();
 }
}

class B extends A{
 public function test(){
  $this->test();
 }
}

$obj = new B();
$obj->test();

Output

Fatal error:  Allowed memory size of 134217728 bytes exhausted
  (tried to allocate 130968 bytes) in [...][...] on line

I was expecting the function redeclaration error instead of memory exhausted error since both the parent and derived class and parent class contains same method.Can somebody explain the reason behind this.?

A: when you do : 
class B extends A{
 public function test(){
  $this->test();
 }
}

It's a recursive call so it's an infinity loop, it's why you have an error or Allowed memory size
Look at this code to understand : 
class A{
  public function who(){
   echo __CLASS__;
 }
 public function test(){
   echo $this->who();
 }
}

class B extends A{
 public function test(){
  parent::test();
 }
}

$obj = new B();
$obj->test();

If you call the method $this->test(), php will not take the "parent" class.
Look at the "extends" documentation.
http://www.php.net/manual/en/keyword.extends.php

A: You are calling test() as recursive calls, 
public function test(){
   $this->test();
 }


A: You are doing recursive, i think you need call parent::
class A{
  public function who(){
   echo __CLASS__;
 }
 public function test(){
   echo $this->who();
 }
}

class B extends A{
 public function test(){
  parent::test();
 }
}

$obj = new B();
$obj->test();


A: The test() is getting called again and again, if you want to call the parent test method use parent::test()
The B class is extending class A and and the function test() is overridden in class B, so unless the function definition changes you do not need to re define the method in child class.
Once you do 
$obj = new B();
$obj->test();

It will check if the definition exists in Class B and if not it will check in parent class A, and since the function is defined there you will get the output 

A: There is not point of calling $this->test() in test() of the child class. If you want to use exactly the same declaration of the method, just don't declare test() in the child class. Since it inherits the methods, you can delete the test() method from B and still you can call B->test(), so it will call the parent class method.
If you want to do any other things and then call the parent method, you can simply do:
class B extends A{
 public function test(){
  //some other things
  parent::test();
 }
}

