2012-05-18 74 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这是错误的,你只能迭代一个数组。