5

I have an array and want to apply MySQLi->real_escape_string on every member of the array through array_walk but this is not working:

array_walk($array, '$mysqli->real_escape_string');

It gives this error:

Warning: array_walk() expects parameter 2 to be a valid callback, function '$mysqli->real_escape_string' not found or invalid function name in C:\wamp\www\ts.php on line 69

$mysqli is a valid object and works fine if I do $mysqli->real_escape_string('anything') on anything else.

My Question: Is it not possible to pass object's functions as callback ? Or am I doing something wrong.


IMPORTANT: I know I can create my own callback function and implement $mysqli->real_escape_string in it BUT I want to know is it not possible to use callback as an object's function ?

0

5 Answers 5

11

If your calling a method within an object you should pass in an array, first item being the object / context and then second should be the method:

Small example

function callback()
{
   //blah
}

the above is called a function and should be called like so: array_walk($array, 'callback');

class object()
{
    public function callback()
    {
    }
}

the above callback is called a method, its practically the same as a function but because its within a class it has a parent context, so should be called like so:

$object = new object();
array_walk($array, array($object , 'callback'));

MySQLi is an object orientated library so after you have initialized your mysqli object you should call the "method" like so:

array_walk($array, array($msqli, 'real_escape_string'));

Also as mentioned above, array_walk will walk both key and value into the mysql object witch will cause in exact escaping, you should use array_map to walk the values alone:

array_map($array, array($msqli, 'real_escape_string'));

Sign up to request clarification or add additional context in comments.

Comments

9

As you can read on php callback page, you shall use:

# produces an error
array_walk($array, array($msqli, 'real_escape_string'));
array_map($array, array($msqli, 'real_escape_string'));

3 Comments

Nope, doesn't work. It says: Warning: array_walk() expects parameter 2 to be a valid callback, non-static method mysqli::real_escape_string() cannot be called statically
That was with quotes and when using EXACTLY how you wrote, it says: Warning: mysqli::real_escape_string() expects exactly 1 parameter, 2 given
With array_map, it seems to work (php.net/manual/en/function.array-map.php#63279)
0

array_walk will only allow a user defined function to be passed as the callback, not a core PHP function or method. To do this I would try the following:

foreach($array as &$value) {
    $value = $mysqli->real_escape_string($value);
}

Passing the value by reference allows it to be modified within the foreach loop, resulting in each member of the array being escaped.

2 Comments

I know I can do that. I am just asking is there no way to pass non-static methods ?
The problem is with array_walk as it can only be used with a user defined function or method. Try array_map as Aif suggests, passing the object and method name as an array as Aif also suggests.
0

I found this post to be quite useful when figuring out how to get array_walk() to work in methods within a class. Adding it to this thread in case it helps others out.

Comments

0

Not directly related, but relevant for people who find this post (like me!) because they got the OP's error message:

If your function is in a namespace, you must include the namespace, e.g.

array_walk($myArray, "myNamespace\myFunction");

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.