2017-10-19 69 views
0

你好,我在使用PHP苗條框架,我收到以下異常:修身例外:標識符「myNonWorkingVariable」沒有定義

Slim Application Error 
The application could not run because of the following error: 

Details 

Type: Slim\Exception\ContainerValueNotFoundException 
Message: Identifier "myNonWorkingVariable" is not defined. 

我無法理解爲什麼我收到此錯誤因爲執行類似任務的所有其他變量以及與此變量類似使用的其他變量正常工作正常,沒有任何錯誤。

下面是我的課:

<?php 


namespace Project\CF; 


use Project\CM\Package\MySomeClass; 

class MyClass 
{ 

    protected $parts; 
    protected $somePDO; 
    protected $myNonWorkingVariable; 
    protected $workingVariable; 

    private function __construct(PartsAndPDO $pnp, $type, $value) 
    { 
     $this->parts = $pnp->getParts(); 
     $this->somePDO = $pnp->getSomePDO(); 
     $this->myNonWorkingVariable = $type; 
     $this->workingVariable = $value; 
    } 

    function get() 
    { 
     return function ($request, $response, $next) { 

      $type = $this->myNonWorkingVariable; 
      /** 
      * The above line throws the following exception, no matter what variable I use here: 
      * Slim Application Error 
      The application could not run because of the following error: 

      Details 

      Type: Slim\Exception\ContainerValueNotFoundException 
      Message: Identifier "myNonWorkingVariable" is not defined. 
      */ 

      /* The below variables $this->parts, $this->workingVariable works fine in below codebase*/ 
      if (!in_array("some value", $this->parts)) 
       $someClass = new MySomeClass($type); 
       $workWithThis = $someClass->get($this->somePDO, $request, $this->workingVariable); 

       $request = $request->withAttribute('key', $workWithThis); 
      } 

      $response = $next($request, $response); 

      return $response; 
     }; 
    } 
} 

我提供,我已經提到,在$this->parts, $this->workingVariable代碼庫工作正常,其他變量的意見。它們的初始化方式與myNonWorkingVariable完全相同。仍然是myNonWorkingVariable這是造成問題,無論我在哪裏使用它在函數內的任何行。

我也試過在代碼庫以下變化:

$type = $this->workingVariable; 

然後異常變化:

Message: Identifier "workingVariable" is not defined. 

花了幾個小時,到現在我仍然不知道爲什麼會這樣。

任何想法爲什麼會發生這種情況?

回答

0

get函數包含一個匿名函數,它具有永遠不會傳遞給它的參數。

<?php 
namespace Project\CF; 

use Project\CM\Package\MySomeClass; 

class MyClass 
{ 

    protected $parts; 
    protected $somePDO; 
    protected $myNonWorkingVariable; 
    protected $workingVariable; 

    private function __construct(PartsAndPDO $pnp, $type, $value) 
    { 
     $this->parts = $pnp->getParts(); 
     $this->somePDO = $pnp->getSomePDO(); 
     $this->myNonWorkingVariable = $type; 
     $this->workingVariable = $value; 
    } 

    function get($request, $response, $next) { 
    { 
     $type = $this->myNonWorkingVariable; 

     if (!in_array("some value", $this->parts)) 
      $someClass = new MySomeClass($type); 
      $workWithThis = $someClass->get($this->somePDO, $request, $this->workingVariable); 

      $request = $request->withAttribute('key', $workWithThis); 
     } 

     $response = $next($request, $response); 

     return $response; 
    } 
}