php - Is is possible to store a reference to an object method? -


assume class code:

class foo {     function method() {         echo 'works';     } } 

is there way store reference method method of foo instance?

i'm experimenting , fiddling around, goal checking whether php allows call $fooinstance->method() without writing $fooinstance-> every time. know write function wrapper this, i'm more interested in getting reference instance method.

for example, pseudo-code theoretically store $foo->method in $method variable:

$foo = new foo(); $method = $foo->method; //undefined property: foo::$method  $method(); 

apparently, method method , i'm not calling () interpreter thinks i'm looking property doesn't work.

i've read through returning references examples show how return references variables, not methods.

therefore, i've adapted code store anonymous function in variable , return it:

class foo {     function &method() {         $fn = function() {             echo 'works';         };         return $fn;     } }  $foo = new foo(); $method = &$foo->method(); $method(); 

this works, rather ugly. also, there's no neat way call single time, seems require storing returned function in variable prior calling it: $foo->method()(); , ($foo->method())(); syntax errors.

also, i've tried returning anonymous function directly without storing in variable, following notice:

notice: variable references should returned reference

does mean returning/storing reference class instance method impossible/discouraged or overlooking something?


update: don't mind adding getter if necessary, goal getting reference method. i've tried:

class foo {     var $fn = function() {         echo 'works';     };     function &method() {         return $this->fn;     } } 

but unexpected 'function' (t_function) error i'd believe php wisely doesn't allow properties store functions.

i'm starting believe goal isn't achievable without use of ugly hacks eval().

it is. have use array, 2 values: class instance (or string of class name if calling static method) , method name string. documented on callbacks man page:

a method of instantiated object passed array containing object @ index 0 , method name @ index 1.

demo (codepad):

<?php class {     public function abc() {         echo 'called';     } }  $some = new something;  $meth = array($some, 'abc');  $meth(); // 'called' 

note works built-ins require callbacks (codepad):

class filter {     public function dofilter($value) {         return $value !== 3;     } }  $filter = new filter; $test = array(1,2,3,4,5); var_dump(array_filter($test, array($filter, 'dofilter'))); // 'array(1,2,4,5)' 

and static methods -- note 'filter' instead of instance of class first element in array (codepad):

class filter {     public static function dofilter($value) {         return $value !== 3;     } }  $test = array(1,2,3,4,5);  var_dump(array_filter($test, array('filter', 'dofilter'))); // 'array(1,2,4,5)' // -------- or ----------- var_dump(array_filter($test, 'filter::dofilter')); // of php 5.2.3 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -