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 });
Comments
Post a Comment