I asked this question yesterday. The answer solved my problem but here is what i'm dealing with now.
I have this array in my class:
private static $menus = [];
Here is a function to addChild to this array:
public static function addChild($item_id, $title, $url, $parent_id, &$array)
{
$child = [
"id" => $item_id,
"title" => $title,
"url" => $url,
"children" => [],
"parent" => $parent_id
];
foreach ($array as $key => &$value) {
if (is_array($value)) {
self::addChild($item_id, $title, $url, $parent_id, $value);
}
if ($key == "id" && $value == $parent_id) {
array_push($array["children"], $child);
}
}
}
The last parameter of this function is an array passed by reference. What I want is to remove this parameter from the function and use the static array of the same class as reference.
Here is what I've tried to do:
public static function addChild($item_id, $title, $url, $parent_id, &$array = self::$menus)
But php does not allows me to do so.
I've also tried this:
public static function addChild($item_id, $title, $url, $parent_id, &$array = null){
$array = self::$menus;
But i get this error:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 1159168 bytes)
I've just learnt this pass by reference concept so I'm not sure what are the limitations of using it or how to use it properly. Any help would save my day.