i’m playing with PHP 5.3 anonymous functions, and try to emulate the prototype based objects like javascript:
$obj = PrototypeObject::create();
$obj->word = "World";
$obj->prototype(array(
'say' => function ($ins) {
echo "Hello {$ins->word}\n";
}
));
$obj->say();
This puts “Hello World”, the first param is the instance of the class (like python) but i want use the this variable, when i call function i do:
$params = array_merge(array($this),$params);
call_user_func_array($this->_members[$order], $params);
And try it, with out results:
call_user_func_array($this->_members[$order] use ($this), $params);
Too try, in __set method:
$this->_members[$var] use ($this) = $val;
And
$this->_members[$var] = $val use ($this);
Any ideas?
The parent’s scope is inherited by
usewhen the anonymous function is created. So what you are trying to do is not possible.Perhaps something more like this is what you want:
But the anonymous function will not be part of the class, and as such, even if you were to pass “
$this” as a parameter via$obj, it would not be able to access the object’s private data.