为什么我不能在非匿名函数中使用'use'?

时间:2022-11-22 22:30:26

I'm trying to create a path from an array of parent-child elements.

我正在尝试从父子元素数组创建一个路径。

The idea was to write a recursive function, which fills up an array with all the elements the path contains in order.

我们的想法是编写一个递归函数,该函数使用路径包含的所有元素按顺序填充数组。

My problem is with closure in PHP:

我的问题是在PHP中关闭:

To get my recursive function to work, I had to define several variables in the global scope.

为了使我的递归函数起作用,我必须在全局范围内定义几个变量。

This is how it looks like:

这是它的样子:

global $breadcrumbs;
$breadcrumbs = array();
function buildBreadcrumbs($elements, $parentID){
    global $siteroot;
    global $breadcrumbs;
    global $navigation;
    if($siteroot['id'] === $parentID){
        $nav = array_values($navigation);
        array_unshift($breadcrumbs, array('label' => 'Start', 'id' => $nav[0]['id']));
    } else {
        foreach ($elements as $element) {
            if ($element['id'] === $parentID) {
                array_unshift($breadcrumbs, array('label' => $element['navlabel'], 'id' => $element['id']));
                buildBreadcrumbs($elements, $element['parent'][0]);
            }
        }
    }
}

I tried using the 'use' keyword instead of globals in this way:

我尝试以这种方式使用'use'关键字而不是globals:

function buildBreadcrumbs($elements, $parentID) use($siteroot, $breadcrumbs, $navigation){
        if($siteroot['id'] === $parentID){
            $nav = array_values($navigation);
            array_unshift($breadcrumbs, array('label' => 'Start', 'id' => $nav[0]['id']));
        } else {
            foreach ($elements as $element) {
                if ($element['id'] === $parentID) {
                    array_unshift($breadcrumbs, array('label' => $element['navlabel'], 'id' => $element['id']));
                    buildBreadcrumbs($elements, $element['parent'][0]);
                }
            }
        }
    }

But this gives me syntax error:

但是这给了我语法错误:

PHP Parse error: syntax error, unexpected T_USE, expecting '{'

What am I doing wrong here?

我在这做错了什么?

Why does $breadcrumbs have to be global in the first place so the function can use it?

为什么$ breadcrumbs必须首先是全局的,所以函数可以使用它?

2 个解决方案

#1


0  

If your variables are all only used in this class, you can use the $this keyword.

如果您的变量都只在此类中使用,则可以使用$ this关键字。

Example:

private $breadcrumbs = array();
private $siteroot;
private $navigation;

function buildBreadcrumbs($elements, $parentID){
    if($this->siteroot['id'] === $parentID){
        $nav = array_values($this->navigation);
        array_unshift($this->breadcrumbs, array('label' => 'Start', 'id' => $nav[0]['id']));
    } else {
        foreach ($elements as $element) {
            if ($element['id'] === $parentID) {
                array_unshift($this->breadcrumbs, array('label' => $element['navlabel'], 'id' => $element['id']));
                buildBreadcrumbs($elements, $element['parent'][0]);
            }
        }
    }
}

If they are truly global variables then you have to use them as global. Or create a function after this that updates your global variables with your class variables (which you updated with the $this keyword.)

如果它们是真正的全局变量,那么您必须将它们用作全局变量。或者在此之后创建一个函数,用您的类变量(使用$ this关键字更新)更新全局变量。

#2


0  

With anonymous functions, you aren't importing variables from the global scope, you're importing variables from the parent scope.

对于匿名函数,您不是从全局范围导入变量,而是从父范围导入变量。

A big distinction would be:

一个很大的区别是:

$global = 123;
function parent() {
   $parent = 123;
   $closure = function() use ($parent, $global) {
      // $global won't exist, but $parent will.
   }
}

A named function doesn't have the same "parent" scope as closures.

命名函数与闭包具有相同的“父”范围。

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared

从父作用域继承变量与使用全局变量不同。全局变量存在于全局范围内,无论执行什么功能,它都是相同的。闭包的父作用域是声明闭包的函数

See example #4 here: http://php.net/manual/en/functions.anonymous.php

请参见示例#4:http://php.net/manual/en/functions.anonymous.php

#1


0  

If your variables are all only used in this class, you can use the $this keyword.

如果您的变量都只在此类中使用,则可以使用$ this关键字。

Example:

private $breadcrumbs = array();
private $siteroot;
private $navigation;

function buildBreadcrumbs($elements, $parentID){
    if($this->siteroot['id'] === $parentID){
        $nav = array_values($this->navigation);
        array_unshift($this->breadcrumbs, array('label' => 'Start', 'id' => $nav[0]['id']));
    } else {
        foreach ($elements as $element) {
            if ($element['id'] === $parentID) {
                array_unshift($this->breadcrumbs, array('label' => $element['navlabel'], 'id' => $element['id']));
                buildBreadcrumbs($elements, $element['parent'][0]);
            }
        }
    }
}

If they are truly global variables then you have to use them as global. Or create a function after this that updates your global variables with your class variables (which you updated with the $this keyword.)

如果它们是真正的全局变量,那么您必须将它们用作全局变量。或者在此之后创建一个函数,用您的类变量(使用$ this关键字更新)更新全局变量。

#2


0  

With anonymous functions, you aren't importing variables from the global scope, you're importing variables from the parent scope.

对于匿名函数,您不是从全局范围导入变量,而是从父范围导入变量。

A big distinction would be:

一个很大的区别是:

$global = 123;
function parent() {
   $parent = 123;
   $closure = function() use ($parent, $global) {
      // $global won't exist, but $parent will.
   }
}

A named function doesn't have the same "parent" scope as closures.

命名函数与闭包具有相同的“父”范围。

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared

从父作用域继承变量与使用全局变量不同。全局变量存在于全局范围内,无论执行什么功能,它都是相同的。闭包的父作用域是声明闭包的函数

See example #4 here: http://php.net/manual/en/functions.anonymous.php

请参见示例#4:http://php.net/manual/en/functions.anonymous.php