Object return value?

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!'); }

as oppose to something like:

if ($err->err_number > 0) { echo 'Error!'); }
//or
if ($err->hasError()) { echo 'Error!'); }

If there is an instance of Error assigned to a variable, one would assume, perhaps mistakenly, that some error had occurred which that object represents.

On a more practical note, you could make use of the __toString magic method to return some value equating to boolean false (an empty string, or “0”) if there is no error, and true (any other string) if there is. Though how that would be any more beneficial than an explicit method asking about the state of any errors present, I don’t know.

What do you mean by “just checking the object”? If all you need to know is if $err is an object, then you can use is_object.
Otherwise, I guess the answer is no :slight_smile:

So then the simple answer is no :slight_smile:

I could have sworn I read about a magic method that was called when just checking the object… blah

So you’ll have to use a method that will return the information you want from that bunch of error information :slight_smile:

The object will hold a bunch of error information

If you don’t want to use a method, then why do you use an object? Just use a variable $err.