0

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 "\n 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
  • Show more related code, please. But it seems like you're using the $this pointer when $this refers to the Exception you threw, and not Utils. Commented Dec 14, 2013 at 20:26

1 Answer 1

2

$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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I now reduced the class to that function and included the calling function.
@DmitriZaitsev No problem! By the way, static methods can be dangerous because you are using Utils explicity rather than using Dependency Injection. When creating the class that uses Utils you should use Dependency Injection like this: $myObj = new myObj(new Utils()) and then in myObj's constructor, set $this->Utils = $Utils. Then in your function: $this->Utils->message(..etc) There are a lot of reasons that this is the best practice - study Dependency Injection for more info!
Thanks again, yes, I do indeed use dep. injection everywhere except for this class :) I had hard time to make this decision but the reasons is Utils::message() is more readable than $this->utils->message(), the latter is too easy to confuse with $this->otherObject->otherMethod, so this is a way to improve readability. Yes, doesn't make my test beautiful but the code looks nicer. Don't really know what is the best

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.