2017-06-30 2 views
4

親子要素の配列からパスを作成しようとしています。なぜ私は非匿名関数で 'use'を使用できないのですか?

アイデアは、パスに含まれるすべての要素を順番に並べ替える再帰関数を作成することでした。

私の問題は、PHPで閉鎖している:

動作するように私の再帰関数を取得するには、私はグローバルスコープで複数の変数を定義する必要がありました。私はこのようにグローバル変数の代わりに「使用」キーワードを使用してみました

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

これは次のように、それがどのように見えるかです

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

しかし、これは私に構文エラーが得られます。

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

私はここで間違っていますか?

$breadcrumbsはなぜ機能が使用できるように最初にグローバルにする必要がありますか?

+2

機能自体にローカルであるあなたがそれにパラメータを追加することを選択しない限り、それは、それ以外の変数にアクセスすることはできません。そのため、$ breadcrumbsにアクセスすることができません - 範囲外ですので – ThisGuyHasTwoThumbs

+0

なぜvarsを関数に渡していませんか? – AbraCadaver

答えて

0

変数がすべてこのクラスでのみ使用されている場合は、$ thisキーワードを使用できます。

例:

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

彼らは真のグローバル変数であれば、あなたは、グローバルとしてそれらを使用する必要があります。または、この変数を使用してグローバル変数をクラス変数(これは$ thisキーワードで更新)で更新します。

0

匿名関数を使用すると、変数をインポートすることはできません。親スコープから削除します。

大きな違いは次のようになります。

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

という名前の関数は、クロージャと同じ「親」スコープを持っていません。

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

ここでは例#4を参照してください:http://php.net/manual/en/functions.anonymous.php

関連する問題