note 28588 added to language.oop.constructor
| From: | steffen at rack1 dot php dot net | Date: | Fri, 17 Jan 2003 14:11:39 +0000 |
| Subject: | note 28588 added to language.oop.constructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42275@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/language.oop.constructor.php
http://master.php.net/manage/user-notes.php?action=edit+28588
http://master.php.net/manage/user-notes.php?action=delete+28588
http://master.php.net/manage/user-notes.php?action=reject+28588