Is it possible to perform a recursion using 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 get this error on execution:
Severity Level: Warning
Message: array_map () expects parameter 1 to be a valid callback, function 'objectToArray' not found or function name invalid.
Thank!
try it
return array_map(['Helpers', 'objectToArray'], $obj);
array_map enable callable.
callable
You can try it with magic constants
return array_map([__CLASS__, __METHOD__], $obj);
Or using self
self
return array_map([self, __METHOD__], $obj);
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);
:
return array_map('self::objectToArray', $obj);
//
return array_map(array(self, 'objectToArray'), $obj);
( json-, ):
class Helpers { public static function objectToArray($obj) { return json_decode(json_encode($obj), true); } }
Source: https://habr.com/ru/post/1531698/More articles:ListView ArrayAdapter hiding children inside a string? - androidHow to change grails.exe console colors on Windows? - windowsGet Enum DisplayName in WebGrid - c #Fighting for binding local images in MvxImageView with MvvmCross - androidReverse lookup hash table to find the smallest key - javaShuffle in WordPress Loop? - loopsLINQ: why different iterators are used in the array and list - performanceR kmeans NAs with an external function error (arg 13) - r46 million element kmeans forces NA - rHow to set text as Placeholder in JTextfield in swing - javaAll Articles