Is it possible to do a recursion on a static method?
class Helpers {
public static function objectToArray($obj) {
if (is_object($obj)) {
$obj = get_object_vars($obj);
}
if (is_array($obj)) {
return array_map(__FUNCTION__, $obj);
}
else {
return $obj;
}
}
}
I'm getting this error when executed:
Severity: Warning
Message: array_map() expects parameter 1 to be a valid callback, function 'objectToArray' not found or invalid function name.
Thanks!
You can recursively call a static method. The following code, for example, will work fine:
<?php
class MyClass{
public static function test($count)
{
if ($count < 10) {
$count++;
self::test($count);
echo $count;
}
}
}
MyClass::test(0);
Try this
return array_map(['Helpers', 'objectToArray'], $obj);
array_map permit callable
type.
You could try it with magic constants
return array_map([__CLASS__, __METHOD__], $obj);
Or using self
return array_map([self, __METHOD__], $obj);
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