无法识别的静态方法-顺序是否重要


Unrecognized static methods - does the order matter?

我有错误

Call to undefined method Exception::message()

调用的对象内部

Utils::message()

(我正在捕捉异常并替换为消息)

包含该对象的文件与定义Utils的另一个文件一起被顶部文件包括(require_once)。

所以我的问题是为什么Utils的方法没有被认可。所有类都包含在主代码之前。包含顺序重要吗?还有其他提示吗?

编辑。我的Utils类:

类Utils{私有静态$count;private static$aggregateCount,$time,$last,$previor;。。。公共静态函数消息(){echo"''n计数:",self::$Count++,";$array=func_get_args();foreach($array作为$entry){if(is_array($entry)){echo print_r($entry);}其他{echo$entry;}}}。。。}

下面是使用类的函数:

公共函数updateManyWithId($schema,array$bundle){foreach($bundle作为$hash){$id=$hash[id_KEY];$hash=$this->_adjustId($schema,$hash);尝试{$this->update($schema,$id,$hash);}catch(异常$e){Utils::message("无法更新哈希:",$hash,$e->message());}}}

$e->message()是问题所在。这指的是一个名为Exception的类,该类应该有一个称为message的方法,但您的Exception类没有该方法。

看看Exception的构造函数。当你扔信息的时候,你在传递信息,也许你在寻找$e->getMessage()

阅读此处:http://www.php.net/manual/en/exception.getmessage.php

总之,考虑一下:

class Exception {
  //construct is used when you throw new Exception (instantiate the class)
  public function __construct($message) {
    $this->message = $message; //now the object has a "message" property
  }
  //this is a "getter" for this object's message
  public function getMessage() {
    return $this->message; //return the message
  }
}

请注意,getters/setters的标准用法是使用getPropertysetProperty作为返回/设置该属性的方法名。