Click to See Complete Forum and Search --> : array_walk -- syntax question


AlionSolutions
November 24th, 2004, 03:26 PM
Hi again,
I am totally fed up by a syntax-problem I am not able to get around:

Let's say, I have a class like this:


class myclass
{
var $arr = array(//SOME ARRAY INITS HERE);

function format($item,$key){// to be done}

function generate()
{
// HERE IS THE PLACE FOR A ARRAY_WALK CALL
// WHICH SHOULD CALL THE CLASS-FUNCTION format.
}
}


Ok, my question is, how is the correct syntax for the mentioned array_walk-call ? I tried several things but got errors each time. The problem is the user-function-parameter.

To describe it in short one more time: How is the syntax of a array_walk-call inside the classes generate function. Especially, how is the syntax for the user-fuction-parameter. It would be easy if this user function was no class member function.


Thanx in advance

alionsonny

khp
November 24th, 2004, 04:10 PM
Have you tried looking up the documentation ?

http://www.php.net/array_walk

AlionSolutions
November 24th, 2004, 04:16 PM
Yes, for sure. This is the first place I look ever. But there's no description about the syntax, if the user-function-parameter is a member function of a class.

It is not allways so, that if a question looks simple, the questioner didn't look into the documentation. :eek:

khp
November 24th, 2004, 04:31 PM
Apparently you did not look very hard

copy and paste from the above linked page

Let's assume that we have a class named 'Search', in which there is a method called 'convertKeywords'. Here's how you would call that convertKeywords method from inside the class:

array_walk($keywords, array($this, 'convertKeywords'));

Notice that, instead of giving a string as the second argument, you give an array with two items: the variable that holds the class (in this case, $this), and the method to call. Here's what it would look like if you were to call convertKeywords from an already-instantiated class:

$search = new Search;
array_walk($keywords, array($search, 'convertKeywords'));

khp
November 24th, 2004, 04:45 PM
It is not allways so, that if a question looks simple, the questioner didn't look into the documentation. :eek:

When someone is kind enogh to point you to the aswer to your question. It would perhaps be a good idear for you to acctually read it. Instead of making blatantly false statemets about the validity of the answer.

visualAd
November 25th, 2004, 08:00 AM
Just one extra thing. If you are going to use an object member function as a callback you need to pass its reference. If you do not then a new object will be instatiated every time the callback function is called.

array_walk($keywords, array(&$this, 'convertKeywords'));