【问题标题】:PHP function pass by reference self::$menusPHP 函数通过引用传递 self::$menus
【发布时间】:2016-09-10 02:08:46
【问题描述】:

我昨天问了this 问题。答案解决了我的问题,但这是我现在正在处理的问题。

我的班级中有这个数组:

private static $menus = [];

这是一个将Child添加到这个数组的函数:

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);
        }
    }
}

这个函数的最后一个参数是一个通过引用传递的数组。我想要的是从函数中删除这个参数,并使用同一个类的静态数组作为引用。

这是我尝试做的:

public static function addChild($item_id, $title, $url, $parent_id, &$array = self::$menus)

但是php不允许我这样做。

我也试过这个:

public static function addChild($item_id, $title, $url, $parent_id, &$array = null){
$array = self::$menus;

但我得到这个错误:

允许内存大小134217728字节用尽(尝试分配1159168字节)

我刚刚学习了这个通过引用传递的概念,所以我不确定使用它有什么限制或如何正确使用它。任何帮助都会挽救我的一天。

【问题讨论】:

  • 除了self::$var 之外的任何东西吗?您是否需要该参数,或者您可以在方法内部引用self::$var
  • 我不想将此变量作为参数传递,但我仍想在运行时更改我的静态数组,因此通过引用传递,但传递类的静态变量。
  • static 并不意味着不能改变,它只是意味着一个类的所有实例将共享完全相同的数据。 const 表示不变,这意味着你不能改变。 stackoverflow.com/questions/1685922/php5-const-vs-static
  • 如果你看到我的链接问题,你就会明白我真正需要什么。

标签: php arrays


【解决方案1】:

您在此处将其作为递归调用传递:

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

这可能会更好:

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

所以如果没有传入任何内容,只需使用static::$menus 而不是$array

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

或者这可能会更好,因为您实际上需要一个数组:

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

然后对于主调用(非递归),只需省略 $array 参数。

【讨论】:

  • 正在做 $array = static::$menus;是否像在通过引用案例中那样更改函数内的数组?
  • 谢谢,它成功了。你能解释一下 self:: vs static:: 这个概念吗?
  • 添加了一些参考资料。
  • 谢谢,你太棒了。
【解决方案2】:

静态方法只能访问静态方法或属性。

self 关键字将对象本身表示为一个实例。静态方法存在于裸类中。

所以不要使用 self:: 而是使用 static:: 它应该可以完成工作。

这是一个完整的例子

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());

【讨论】:

  • 你的意思不是'&$array = self::$menus',我应该这样做:'&$array = static::$menus'?
  • 作为参数的默认值。
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 2012-05-16
  • 2015-07-04
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多