[PHP] array walk and class member functions
Hi I'm batting my head against a wall on this one... I have a class that has a constructor which sets some initial conditions, and then a public function that does some work. I want to be able to call this function from an external array_walk call, but when I try and reference it as $myClass-myFunction in the array_walk call, I get an error back saying that this is an invalid function. eg) ?php include ../includes/aClass.class; $myArray = array(item1=firstItem, item2=secondItem); $myClass = new aClass; array_walk($myArray,'$myClass-aMemberFunction'); ? As a workaround, I redifined the class so that there was a public function which made the array_walk call with the worker function defined internally as follows, but this throws another issue in that if I create multiple instances of the class then I get an error to say that the internal function is already defined ... *Fatal error*: Cannot redeclare aFunction() (previously declared in /usr/local/apache2/htdocs/includes/functions.php:23) in */usr/local/apache2/htdocs/includes/functions.php* on line *23* class aClass { some other stuff, constructor etc public function aPublicFunction($anArray) { global $aRetrunString; function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } array_walk($anArray,'aFunction'); return $aReturnString; } } Can anyone help me to get either solution to a working state? By the way... apache 2.0.49 php-5.0.2 Thanks very much Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
Tom wrote: Hi I'm batting my head against a wall on this one... I have a class that has a constructor which sets some initial conditions, and then a public function that does some work. I want to be able to call this function from an external array_walk call, but when I try and reference it as $myClass-myFunction in the array_walk call, I get an error back saying that this is an invalid function. eg) ?php include ../includes/aClass.class; $myArray = array(item1=firstItem, item2=secondItem); $myClass = new aClass; array_walk($myArray,'$myClass-aMemberFunction'); try changing this line to: array_walk($myArray, array($myClass,'aMemberFunction')); or if you prefer to take the route of your second attempt read on... ? As a workaround, I redifined the class so that there was a public function which made the array_walk call with the worker function defined internally as follows, but this throws another issue in that if I create multiple instances of the class then I get an error to say that the internal function is already defined ... which is correct, you are trying to create this function multiple times. *Fatal error*: Cannot redeclare aFunction() (previously declared in /usr/local/apache2/htdocs/includes/functions.php:23) in */usr/local/apache2/htdocs/includes/functions.php* on line *23* class aClass { some other stuff, constructor etc public function aPublicFunction($anArray) { global $aRetrunString; function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } array_walk($anArray,'aFunction'); return $aReturnString; } wrap the function def. like so: if (!function_exists('aFunction')) { function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } } OR define the function outside of the class e.g. function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } BTW: using a global in the way you do in this function is a bad idea. array_walk() is defined as follows: bool array_walk ( array array, callback funcname [, mixed userdata]) check out the manual for info on the last arg: http://nl2.php.net/array_walk } Can anyone help me to get either solution to a working state? By the way... apache 2.0.49 php-5.0.2 Thanks very much Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
Jochem Maas wrote: Tom wrote: snip Sorry Tom, I hit the send button too early! I mean to add some context to the code below so you would (hopefully) understand what I mean: OR define the function outside of the class e.g. function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } ...was meant to be: function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } class aClass { some other stuff, constructor etc public function aPublicFunction($anArray) { global $aRetrunString; function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } array_walk($anArray,'aFunction'); return $aReturnString; } // rest of the class definition goes here... snip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
Tom wrote: Thanks very much for the help - both methods work fine. I'd had a look at the array_walk manual, but didn't realise that the second param would accept an array - that's really cool indeed that page does not make it very clear, it's the generic call_back syntax, which can be used practically everywhere a callback function is expected, the array you pass can be in the form of: array($object, 'methodname') or array('classname', 'methodname') the second version allows you to use static class methods. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
indeed that page does not make it very clear, it's the generic call_back syntax, which can be used practically everywhere a callback function is expected, the array you pass can be in the form of: array($object, 'methodname') or array('classname', 'methodname') the second version allows you to use static class methods. I never knew about this syntax until I saw it on this newsgroup quite a few months back. Perhaps an update to the manual is in order? (Whistling and walking away...) -- Teach a person to fish... Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html PHP Manual: http://www.php.net/manual/en/index.php php-general archives: http://marc.theaimsgroup.com/?l=php-generalw=2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
Thanks very much for the help - both methods work fine. I'd had a look at the array_walk manual, but didn't realise that the second param would accept an array - that's really cool! I don't like the function_exists method much, as I frequently overload class members. Not needed now as I have the worker defined as a private function and can access it from the public one as array_walk($anArray,array($this,'aFunction'); Thanks Tom Jochem Maas wrote: Tom wrote: Hi I'm batting my head against a wall on this one... I have a class that has a constructor which sets some initial conditions, and then a public function that does some work. I want to be able to call this function from an external array_walk call, but when I try and reference it as $myClass-myFunction in the array_walk call, I get an error back saying that this is an invalid function. eg) ?php include ../includes/aClass.class; $myArray = array(item1=firstItem, item2=secondItem); $myClass = new aClass; array_walk($myArray,'$myClass-aMemberFunction'); try changing this line to: array_walk($myArray, array($myClass,'aMemberFunction')); or if you prefer to take the route of your second attempt read on... ? As a workaround, I redifined the class so that there was a public function which made the array_walk call with the worker function defined internally as follows, but this throws another issue in that if I create multiple instances of the class then I get an error to say that the internal function is already defined ... which is correct, you are trying to create this function multiple times. *Fatal error*: Cannot redeclare aFunction() (previously declared in /usr/local/apache2/htdocs/includes/functions.php:23) in */usr/local/apache2/htdocs/includes/functions.php* on line *23* class aClass { some other stuff, constructor etc public function aPublicFunction($anArray) { global $aRetrunString; function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } array_walk($anArray,'aFunction'); return $aReturnString; } wrap the function def. like so: if (!function_exists('aFunction')) { function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } } OR define the function outside of the class e.g. function aFunction($value, $key) { global $aReturnString; $aReturnString = $aReturnString.$value; } BTW: using a global in the way you do in this function is a bad idea. array_walk() is defined as follows: bool array_walk ( array array, callback funcname [, mixed userdata]) check out the manual for info on the last arg: http://nl2.php.net/array_walk } Can anyone help me to get either solution to a working state? By the way... apache 2.0.49 php-5.0.2 Thanks very much Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array walk and class member functions
Jason Barnett wrote: indeed that page does not make it very clear, it's the generic call_back syntax, which can be used practically everywhere a callback function is expected, the array you pass can be in the form of: array($object, 'methodname') or array('classname', 'methodname') the second version allows you to use static class methods. I never knew about this syntax until I saw it on this newsgroup quite a few months back. Perhaps an update to the manual is in order? (Whistling and walking away...) perhaps not: http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php