note 94804 modified in functions.anonymous by danbrown

From: Date: Wed, 25 Nov 2009 18:28:05 +0000
Subject: note 94804 modified in functions.anonymous by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163286@lists.php.net to get a copy of this message
You can always call protected members using the __call() method - similar to how you hack around this in Ruby using send. <?php class Fun { protected function debug($message) { echo "DEBUG: $message\n"; } public function yield_something($callback) { return $callback("Soemthing!!"); } public function having_fun() { $self =& $this; return $this->yield_something(function($data) use (&$self) { $self->debug("Doing stuff to the data"); // do something with $data $self->debug("Finished doing stuff with the data."); }); } // Ah-Ha! public function __call($method, $args = array()) { if(is_callable(array($this, $method))) return call_user_func_array(array($this, $method), $args); } } $fun = new Fun(); echo $fun->having_fun(); ?> --was-- You can always call protected members using the __call() method - similar to how you hack around this in Ruby using send. <?php class Fun { protected function debug($message) { echo "DEBUG: $message"; } public function yield_something($callback) { return $callback("Soemthing!!"); } public function having_fun() { $self =& $this; return $this->yield_something(function($data) use (&$self) { $self->__call('debug', "Doing stuff to the data"); // do something with $data $self->__call('debug', "Finished doing stuff with the data."); }); } // Ah-Ha! public function __call($method, $args = array()) { if(is_callable(array($this, $method))) return call_user_func(array($this, $method), $args); } } $fun = new Fun(); echo $fun->having_fun(); ?> http://php.net/manual/en/functions.anonymous.php

« previous php.notes (#163286) next »