Run
Run

Reputation: 57176

PHP anonymous functions: how to pass $this and write a fallback?

$this is not supported for anonymous functions inside a class for php5.3.x, how do I get around to this so I can pass the $this info/ data to the anonymous function? How can I write a fallback to php5.3.x?

class anonymous {

    protected $methods = array();

    public function __construct(array $options)
    {
        $this->methods = $options;
    }

    public function __call($name, $arguments)
    {
        $callable = null;
        if (array_key_exists($name, $this->methods))
            $callable = $this->methods[$name];
        elseif(isset($this->$name))
            $callable = $this->$name;

        if (!is_callable($callable))
            throw new BadMethodCallException("Method {$name} does not exists");

        return call_user_func_array($callable, $arguments);
    }
}

class myclass {

    private $options = array();

    public function __construct($options = array()){
        $this->options = $options;
    }

    public function hello($data = null, $options = array()){

        $methods = new anonymous(array(
            "init" => function($options) { 

                echo "init";

                if(!$options) $options = $this->options;

                return $options; 
            }, 

            "run" => function($options) { 
                echo "run";
                return $options; 
            } 
        ));

        $default = array(
            "method" => "init",
            "options" => array()
        );

        if($data === null) {
            $method = "init";
        } else if (is_string($data)) { 
            $method = $data;
        } else if(is_array($data)) {
            $method = "init";
            $options = $data;
        }

        // Return the requested method.
        return $methods->$method($options);

    }

}

so for,

$myclass = new myclass(array("hello world!"));
var_dump($myclass->hello());

result,

init Fatal error: Using $this when not in object context in /home/content/95/10799595/html/bin/test/anonymous_4.php on line 41 --> if(!$options) $options = $this->options;

I should be getting this as in my localhost which is on php5.4,

init array (size=1) 0 => string 'hello world!' (length=12)

Any solutions and suggestions?

EDIT:

public function hello($data = null, $options = array()){

        $self = $this;

        $methods = new anonymous(array(
            "init" => function($options) use($self){ 

                echo "init";

                if(!$options) $options = $self->options;

                return $options; 
            }, 

            "run" => function($options) { 
                echo "run";
                return $options; 
            } 
        ));

        $default = array(
            "method" => "init",
            "options" => array()
        );

        if($data === null) {
            $method = "init";
        } else if (is_string($data)) { 
            $method = $data;
        } else if(is_array($data)) {
            $method = "init";
            $options = $data;
        }

        // Return the requested method.
        return $methods->$method($options);

    }

still get an error.

Fatal error: Cannot access private property myclass::$options in /home/content/95/10799595/html/bin/test/anonymous_4.php on line 43

Upvotes: 0

Views: 690

Answers (1)

Smar
Smar

Reputation: 8591

Just

$foobar = $this;

$anonymous = function() use($foobar) {

};

works.

It’s silly, but that’s php... :)

Upvotes: 2

Related Questions