2017-10-19 21 views
0

Bonjour J'utilise cadre mince en PHP et je reçois l'exception suivante:Exception Slim: Identifier « myNonWorkingVariable » n'est pas défini

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

Details 

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

Je ne suis pas en mesure de comprendre pourquoi je reçois cette erreur car toutes les autres variables qui effectuent des tâches similaires et sont utilisées de la même manière que cette variable fonctionnent correctement sans aucune erreur.

Ci-dessous est ma classe:

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

J'ai fourni des commentaires où je viens de mentionner que d'autres variables $this->parts, $this->workingVariable fonctionne très bien dans codebase. Ils sont également initialisés de la même manière que myNonWorkingVariable. Pourtant, c'est myNonWorkingVariable qui pose problème, peu importe où je l'utilise dans n'importe quelle ligne à l'intérieur de la fonction.

J'ai essayé aussi le changement suivant dans codebase:

$type = $this->workingVariable; 

Ensuite, les changements d'exception à:

Message: Identifier "workingVariable" is not defined. 

quelques heures passées jusqu'à ce que j'ai maintenant encore aucune idée pourquoi cela se passe.

Une idée pourquoi cela se produit-il?

Répondre

0

La fonction get contenait une fonction anonyme, avec des paramètres qui ne lui seraient jamais transmis.

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