1
votes

I am using the overloading in PHP, whats the problem with my PHP code even its only 8 lines of code:

class c1{
  public function __call($name,$array){
      if($this->$name()){ return true;}
  }
}

$cl = new c1;
echo $cl->m1();

and it says:

"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes)"

1
You attempt to call m1(), __call() is invoked and tries to call m1() __call() is invoked and tries to call m1() etc... Endless loop. At least endless until memory is exhausted.AbraCadaver
You might want if(method_exists($this, $name)){ return true;}AbraCadaver

1 Answers

6
votes

You're making an infinite loop with this code :

if($this->$name()){ return true;}

This line makes a call to __call, that makes a call to __call, and so on, because the function "m1" is not defined.