4

I was trying to use array_walk_recursive for something, and wanted to use one of the class' methods as the call back, so trying:

  array_walk_recursive($TAINTED, "$this->encode()");

and variations thereof all failed. I eventually settled for:

array_walk_recursive($TAINTED, 'className::encode');

which works, but I've read on here that calling class methods in a static fashion like this is often considered poor practice. Or is this one of those situations where it's necessary?

So, is this the right way to go about it, or is there a way to put in the callback function without having to fall back on using it as a static class method?

2 Answers 2

12
array_walk_recursive($TAINTED, array($this, 'encode'));
Sign up to request clarification or add additional context in comments.

Comments

1

I know this thread is older but by reading your words "calling class methods in a static fashion like this is often considered poor practice" I have to say that static functions are a good practice when used for the right task. Frameworks like Laravel and Symphony shows the true potential of static methods.

Anyways when you aren't afraid of using static methods you can call your method using self instead of specifying the class name that might change during development process.

array_walk_recursive($TAINTED, 'self::encode'); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.