2017-10-19 71 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; 
    } 
}