Is there some special method or something that can return a value when checking an object? For example, say I have an Error class. $err = new Error(); $err->err_number = 102; I would like to check something like: if ($err) { echo 'Error!'); } PHP: as oppose to something like: if ($err->err_number > 0) { echo 'Error!'); } //or if ($err->hasError()) { echo 'Error!'); } PHP:
Hmm, you could create a __toString() function which will return a string with the error message? Like this: <?php class Error { public function __toString() { return $this->error_msg; } ?> PHP:
If err_number if a public variable, you set another variable to it: $err = new Error(); $err->err_number = 102; $error =& $err->err_number; if ($error ) { echo 'Error!'); } unset($error); // or unset($err->err_number); if ($error ) { echo 'Error!'); } PHP: