note 98384 added to functions.anonymous

From: Date: Fri, 11 Jun 2010 17:50:36 +0000
Subject: note 98384 added to functions.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-170069@lists.php.net to get a copy of this message
$this is currently (PHP 5.3.2) not usable directly with closures. One can write: <?php $self = $this; function () use ($self) { ... } ?> but then the private/protected members of $this cannot be used inside the closure. This makes closures much less useful in OO code. Until this is fixed, one can cheat using reflection: <?php class FullAccessWrapper { protected $_self; protected $_refl; public function __construct($self) { $this->_self = $self; $this->_refl = new ReflectionObject($self); } public function __call($method, $args) { $mrefl = $this->_refl->getMethod($method); $mrefl->setAccessible(true); return $mrefl->invokeArgs($this->_self, $args); } public function __set($name, $value) { $prefl = $this->_refl->getProperty($name); $prefl->setAccessible(true); $prefl->setValue($this->_self, $value); } public function __get($name) { $prefl = $this->_refl->getProperty($name); $prefl->setAccessible(true); return $prefl->getValue($this->_self); } public function __isset($name) { $value = $this->__get($name); return isset($value); } } /** * Usage: * $self = giveAccess($this); * function() use ($self) { $self->privateMember... } */ function giveAccess($obj) { return new FullAccessWrapper($obj); } // Example: class Foo { private $x = 3; private function f() { return 15; } public function getClosureUsingPrivates() { $self = giveAccess($this); return function () use ($self) { return $self->x * $self->f(); }; } } $foo = new Foo(); $closure = $foo->getClosureUsingPrivates(); echo $closure() . "\n"; // Prints 45 as expected ---- Server IP: 80.81.171.181 Probable Submitter: 213.157.91.244 ---- Manual Page -- http://www.php.net/manual/en/functions.anonymous.php Edit -- https://master.php.net/note/edit/98384 Del: integrated -- https://master.php.net/note/delete/98384/integrated Del: useless -- https://master.php.net/note/delete/98384/useless Del: bad code -- https://master.php.net/note/delete/98384/bad+code Del: spam -- https://master.php.net/note/delete/98384/spam Del: non-english -- https://master.php.net/note/delete/98384/non-english Del: in docs -- https://master.php.net/note/delete/98384/in+docs Del: other reasons-- https://master.php.net/note/delete/98384 Reject -- https://master.php.net/note/reject/98384 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170069) next »