Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Closure On Class Instance

If I have a class like:

class MyClass
{
   public function foo() 
   {
      echo "foo";
   }
}

And then outside of the class instantiate it and try to create an anonymous function in it:

$mine = new MyClass();

$mine->bar = function() {
   echo "bar";
}

And then try to call it like $mine->bar(), I get:

Fatal error: Call to undefined method MyClass::bar() in ...

How can I create an anonymous function / closure on a class instance?

Aside: Before you tell me I should rethink my logic or use interfaces and OOP properly, in my case, it's a convenience method that applies to this specific instance of a bastardized class in an attempt to clean-up a legacy procedural application. And yes, I'm using PHP 5.3+

like image 629
Jeremy Harris Avatar asked Aug 13 '26 04:08

Jeremy Harris


1 Answers

See my blog article here: http://blog.flowl.info/2013/php-container-class-anonymous-function-lambda-support/

You need to add a magic __call function:

public function __call($func, $args) {
    return call_user_func($this->$func, $args);
}

The problem is that within this construct you can call private methods from public scope. I suggest not to simply add new variables to a class that are not defined. You can avoid this using magic __set functions and catch all undefined variables in a container (= array, like in my blog post) and change the call_user_func behaviour to call only inside the array:

// inside class:
public $members = array();

public function __call($func, $args) {
    // note the difference of calling only inside members:
    return call_user_func($this->members[$func], $args);
}
like image 110
Daniel W. Avatar answered Aug 14 '26 20:08

Daniel W.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!