1

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.

Community
  • 1
  • 1
Subhan
  • 1,544
  • 3
  • 25
  • 58
  • Will it ever be anything other than `self::$var`? Do you need that parameter at all, or can you just reference `self::$var` inside of the method? – Jeff Lambert May 13 '16 at 18:58
  • I don't want to pass this variable as parameter, but i still want to change my static array at run time so pass by reference but the static variable of a class. – Subhan May 13 '16 at 18:59
  • `static` doesn't mean can't change, it just means that all instances of a class will share the exact same data. `const` means constant, which means you can't change. http://stackoverflow.com/questions/1685922/php5-const-vs-static – Jeff Lambert May 13 '16 at 19:02
  • If you see my linked question, you will understand what i actually needed. – Subhan May 13 '16 at 19:03

2 Answers2

2

You are passing it in as a recursive call here:

self::addChild($item_id, $title, $url, $parent_id, $value);

Which might be better as:

static::addChild($item_id, $title, $url, $parent_id, $value);

So just use static::$menus instead of $array if nothing was passed in:

public static function addChild($item_id, $title, $url, $parent_id, &$array=null)
{
    if($array === null) {
        $array = &static::$menus;
    }
    // other code
}

Or this might be better since you actually need an array:

if(!is_array($array)) {
    $array = &static::$menus;
}

Then for the main calls (not recursive) just omit the $array parameter.

Community
  • 1
  • 1
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

static method can only access static methods or properties.

self key word represent the object itself as an instance. Static method exists on bare class.

So instead of self:: just use static:: and it should do the work.

Here's an full example

class Test {
    private static $menus = [];
    // Here is a function to addChild to this array:

    public static function addChild($item_id, $title, $url, $parent_id, &$array = null)
    {
        if(is_null($array))
        {
            $array = &static::$menus;
        }
        $child = [
            "id" => $item_id,
            "title" => $title,
            "url" => $url,
            "children" => [],
            "parent" => $parent_id
        ];
        foreach ($array as $key => &$value)
        {
            if (is_array($value))
            {
                static::addChild($item_id, $title, $url, $parent_id, $value);
            }
            if ($key == "id" && $value == $parent_id)
            {
                array_push($array["children"], $child);
            }
        }
        if(empty($array))
        {
            $array["children"] = [ $child ];
        }
    }

    public static function getMenus() {
        return static::$menus;
    }

}

Test::addChild(1,1,1,1);
var_export(Test::getMenus());
Richard
  • 1,045
  • 7
  • 11