Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Function/Closure and using self:: or static::

I am working with anonymous functions where I am creating anonymous function outside of the object, and then adding it to an object later in which it will be used with __callStatic magic function. The closures that are being added to contain methods from the parent class. I am wondering if I would be able to call those methods from the closure?

Right now I get this error:

EmptyObject::addMethod('open', function(){
    if (static::_hasAdapter(get_class(), __FUNCTION__))
            return self::_callAdapter(get_class(), __FUNCTION__, $details);

    echo '<p>You have mail!</p>';
});

throws this error:

Fatal error: Cannot access static:: when no class scope is active in

And

//Add the functions
EmptyObject::addMethod('open', function(){
    if (EmptyObject::_hasAdapter('EmptyObject', __FUNCTION__))
            return EmptyObject::_callAdapter('EmptyObject', __FUNCTION__, $details);

    echo '<p>You have mail!</p>';
});

throw this error because the method is protected

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Method '_hasAdapter' was not found in class EmptyObject'

like image 862
Devin Dixon Avatar asked Nov 27 '11 17:11

Devin Dixon


People also ask

Is an anonymous function a closure?

Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.

What is static closure in PHP?

Closure are functions that may be stored in a variable : functions may have their name stored in a variable, though. Closure also have the ability to aggregate variables from the context of their creation, for future use. As such, $this is available in a closure that is created inside an object.

What is anonymous function with example?

An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();

Which is are true about anonymous functions?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.


1 Answers

You can achieve this by using Closure::bind() (PHP >= 5.4.0)

abstract class EmptyObject
{
   protected static $methods = array();

   final public static function __callStatic($name, $arguments)
   {
      return call_user_func(self::$methods[$name], $arguments);
   }

   final public static function addMethod($name, $fn)
   {
      self::$methods[$name] = Closure::bind($fn, NULL, __CLASS__);
   }

   final protected static function protectedMethod()
   {
      echo __METHOD__ . " was called" . PHP_EOL;
   }
}

Now any anonymous function passed to EmptyObject::addMethod() will be run in the scope of the EmptyObject class

EmptyObject::addMethod("test", function()
{
   self::protectedMethod();
});


// will output:
// EmptyObject::protectedMethod was called

EmptyObject::test();
like image 115
Ben Bidner Avatar answered Sep 27 '22 19:09

Ben Bidner