note 28588 deleted from language.oop.constructor by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 18:58:02 +0000 |
| Subject: | note 28588 deleted from language.oop.constructor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162537@lists.php.net to get a copy of this message | ||
Note Submitter: steffen staehle
----
A note on a constructor's return value:
When creating an instance of a class, 'new' obviously returns the object, not the
constructor's return value (if any).
Still, it might be tempting to communicate the success or failure of constructing the object via a
return value:
class A
{
function A()
{
// ...
// some error occurred
return FALSE;
}
}
if ($a = new A())
{
// success, use $a ...
}
THIS WILL NOT WORK the way intended.
'new' returns the object, the assignment returns the value of the assignment, i.e. the
object, and whether an object evaluates as TRUE does not depend on the constructor's return
value.
My preferred solution - set some object property during the construction phase that you can check
against explicitely.