You are here: array walk and class member functions « PHP « IT news, forums, messages
array walk and class member functions

Posted by Tom on 10/14/14 11:05

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация