I have the below method in a singleton class
private function encode($inp)
{
if (is_array($inp) {
return array_map('$this->encode', $inp);
} else if is_scalar($inp) {
return str_replace('%7E', rawurlencode($inp));
} else {
return '';
}
}
this works fine as an ordinary function
function encode($inp)
{
if (is_array($inp) {
return array_map('encode', $inp);
} else if is_scalar($inp) {
return str_replace('%7E', rawurlencode($inp));
} else {
return '';
}
}
when using inside a class i'm getting the below error:
PHP Warning: array_map(): The first argument, '$this->rfc_encode', should be either NULL or a valid callback
Please could anybody help me to fix this.
Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.
Show activity on this post. @interface Robot: NSObject + (void)sayHi:(void(^)(NSString *))callback; @end @implementation Robot + (void)sayHi:(void(^)(NSString *))callback { // Return a message to the callback callback(@"Hello to you too!"); } @end [Robot sayHi:^(NSString *reply){ NSLog(@"%@", reply); }];
A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.
From PHP Manual on Callbacks:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
So try
return array_map(array($this, 'encode'), $inp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With