2012-05-18 2 views
0

안녕하세요 여러분, 저는 지금 돌볼 세 가지를 얻고 있습니다. 그리고 당신이 도울 수 있기를 희망합니다. 내가 가지고있는 오류와 경고, 그리고 코드에있는 내용을 패치하고 싶습니다. 그리고 당신이 저를 도울 수 있기를 희망합니다. 오류는 다음과 같습니다.PHP 경고,주의 및 오류

가 나는 격언을 생각하고

PHP Warning: Invalid argument supplied for foreach() in php/libraries/RotateLatestAssets.class.php on line 35 
PHP Notice: Undefined variable: config in php/libraries/RotateLatestAssets.class.php on line 18 
PHP Notice: Undefined variable: json in php/libraries/RotateLatestAssets.class.php on line 31 
PHP Notice: Undefined variable: json in php/libraries/RotateLatestAssets.class.php on line 32 

그리고 내 코드에서 주석 정말 정말 이상한 일이있다 .... 멋진 오류가 있지만, 사람은 ....

을 알고있다

가 여기에 귀하의 생성자에서 클래스

<?php 

class RotateLatestAssets 
{ 
    protected $config; 

    public function __construct() 
    { 
     $this->config = $config; 
    } 

    //******************************************************************************** 
    //* Public Methods 
    //******************************************************************************** 

    public function getAssets() 
    { 

     $this->json = self::jsonDecode($this->config, true); 


     define('latest', __DIR__ . $json['paths']['latest']); 
     define('dist',  __DIR__ . $json['paths']['dist']); 


     foreach($this->json['files'] as $fileName => $fileVersion) 
     { 
      $cacheName = implode("-$fileVersion.", explode('.',$fileName)); 
      if(!file_exists(dist . $cacheName)) 
      { 
       try { 
        copy(latest . $cacheName, dist . $fileName); 
       } catch(Exception $e) { 
        echo 'Copy Exception: ', $e->getMessage(), "\n"; 
       } 
      } 
     } 

    } 

    //******************************************************************************** 
    //* Static Methods 
    //******************************************************************************** 

    /** 
    * Returns a json decoded object or array 
    * 
    * @param string $json 
    * @param bool $toAssoc 
    * @return object|array Depending on the parameter supplied 
    */ 

    private static function jsonDecode($json, $toAssoc = true) 
    { 

     /** 
     * Based on known JSON_DECODE() errors 
     * 0 = JSON_ERROR_NONE 
     * 1 = JSON_ERROR_DEPTH 
     * 2 = JSON_ERROR_STATE_MISMATCH 
     * 3 = JSON_ERROR_CTRL_CHAR 
     * 4 = JSON_ERROR_SYNTAX 
     * 5 = JSON_ERROR_UTF8 
     */ 

     $result = json_decode($json, $toAssoc); 

     /* Will produce this sometimes out of the blue after a few refreshes? 

      PHP Fatal error: Uncaught exception 'Exception' with message 
      'JSON Error: - Syntax error, malformed JSON' in php/libraries/RotateLatestAssets.class.php:99 

      Stack trace: 
      #0 php/libraries/RotateLatestAssets.class.php(28): RotateLatestAssets::jsonDecode(NULL, true) 
      #1 php/libraries/RotateLatestAssets.class.php(119): RotateLatestAssets->getAssets() 
      #2 {main} 
       thrown in php/libraries/RotateLatestAssets.class.php on line 99 
     */ 

     switch(json_last_error()) 
     { 
      case JSON_ERROR_DEPTH: 
       $error = ' - Maximum stack depth exceeded'; 
       break; 
      case JSON_ERROR_STATE_MISMATCH: 
       $error = ' - Invalid or malformed JSON encoding'; 
       break; 
      case JSON_ERROR_CTRL_CHAR: 
       $error = ' - Unexpected control character found'; 
       break; 
      case JSON_ERROR_SYNTAX: 
       $error = ' - Syntax error, malformed JSON'; 
       break; 
      case JSON_ERROR_UTF8: 
       $error = ' - Syntax error, malformed UTF-8 characters'; 
       break; 
      case JSON_ERROR_NONE: 
      default: 
       $error = ''; 
     } 

     if (!empty($error)) throw new Exception('JSON Error: '.$error); 
     return $result; 
    } 

    /** 
    * Returns bool value for state of array recieved dev:(true) live:(false) 
    * 
    * @param array $array 
    * @return bool 
    */ 

    private static function checkMode($array) 
    { 
     if(!is_array($array)) throw new Exception("Wrong Type Passed. Expecting (array), received (".gettype($array).")"); 
     return ($array['dev'] == 'true') ? true : false; 
    } 
} 


$instance = new RotateLatestAssets($_SERVER['DOCUMENT_ROOT'].'build/config.json'); 
$instance->getAssets(); 

?> 

답변

2

, 당신은이 :

$this->config = $config; 

그러나 $config 변수는 생성되지 않으므로 값이 없습니다.

getAssets()에서 동일한 작업을 수행합니다. $json을 사용하지만 정의 된 적이 없습니다.

잘못된 인수 오류는 본질적으로 foreach(null을 잘못 수행하고 있기 때문에 배열을 반복 할 수만 있습니다.