how do i define delegate in php -


how define function within class , method empty, method change calling class (as delegate in c#) following error codes are:

class myclass{       public $num1=0;       public function func($a,$b); } $c=new myclass(); $c->func=function($a,$b){       return $a+$b; } $c->func(4,8);// error 

you can this:

class myclass {       public $num1=0;       public function func($a,$b,$fn) {             return $fn($a,$b); // return anonymous function       } }  $c = new myclass();  $c->func(2,5,function($a,$b) { // declaring anonymous function $a , $b parameters     echo $a + $b; // result 7 }); 

demo


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -