note 101476 deleted from language.oop5.overloading by cmb

From: Date: Sun, 18 Dec 2016 10:05:42 +0000
Subject: note 101476 deleted from language.oop5.overloading by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208658@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- Using anon functions, you can allow your class methods to be declared after your class is already set. Though you cannot use a class property as a function in any way (including for properties that are objects with their own __invoke() method) without triggering a fatal error, there is a workaround using an array property to store all these anon functions... <?php class Test { // generic class allowing dynamic addition of methods public $anon_list = array(); // list of all custom added methods function __call($fnc, $args) {return call_user_func_array($this->anon_list[$fnc], $args);} function __set($name, $val) {$this->anon_list[$name] = $val;} } // Example adding a new custom method $test = new Test; $test->do_something = function ($var1, $var2) {echo "custom called with $var1 and $var2\n";}; $test->do_something('asdf', 'test'); ?> Unfortunately, without access to $this within the anon functions, it's not really useful. You can "use ($test)" in your anon function, but that just makes it more complicated.

« previous php.notes (#208658) next »