doukong6031 2013-12-14 20:26
浏览 23
已采纳

无法识别的静态方法 - 订单是否重要?

I have the error

Call to undefined method Exception::message()

inside the object calling

Utils::message()

(I am catching the Exception and replacing by the message)

The file containing this object is included (require_once) by the top file together with another file defining Utils.

So my question is why the method of Utils is not recognized. All classes are included before the main code. Does the inclusion order matter? Any other hints?

EDIT. My Utils class:

class Utils {

   private static $count;
   private static $aggregateCount, $time, $last, $previous;

 ...

  public static function message () {
      echo "
 Count: ", self::$count++, " ";
     $array = func_get_args();
     foreach ($array as $entry) {
       if (is_array($entry)) {
         echo print_r($entry);
       } else {
         echo $entry;         
       }
     }
  }

 ...
}

And here is the function using the class:

  public function updateManyWithId ($schema, array $bundle) {
    foreach ($bundle as $hash) {
      $id = $hash[ID_KEY];
      $hash = $this->_adjustId($schema, $hash);
      try {
        $this->update($schema, $id, $hash);
      } catch (Exception $e) {
        Utils::message("Cannot update hash: ", $hash, $e->message());
      }
    }
  }
  • 写回答

1条回答 默认 最新

  • dongzhun4898 2013-12-14 20:30
    关注

    $e->message() Is the problem. That refers to a class called Exception which is expected to have a method called message, but your Exception class does not have that method.

    Look at the constructor of your Exception. You're passing in a message when you throw it, perhaps you're looking for $e->getMessage().

    Read here: http://www.php.net/manual/en/exception.getmessage.php

    To summarize, consider this:

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

    Note that the standard usage of getters/setters is to use getProperty and setProperty as the method name that returns/sets that property.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路