On Apr 14, 2004, at 10:36 PM, Thies C.Arntzen wrote:



Am 14.04.2004 um 21:53 schrieb Marcus Boerger:


Personally I'd much prefer a way of returning a value from a
constructor, i.e. to be able to 'return null;' or a similar language
construct so I could do 'if ($db = new SQLiteDatabase)'
It would also mean that I would run into a 'calling method on a
non-object' error if I don't check it (which is fine).

In no language i know (c++, delphi, java as the popular ones) a ctor
can return a value especially not it's own value. The problem is that
when the ctor is called the object is already created. For most languages
that means some memory has been allocated and for php overloaded objects
like the SQLite class it also means that the memory has been initialized.


Now in PHP 4 there was the ugly trick of setting "$this=NULL".


i'm sure ZE2 can be tricked to support "return NULL" from a constructor so that:


$a = new bla();

if (! $a) ...


if ctors are the only place that *cannot* life without exceptions it would be well worth *fixing* (yes, i said fix), as adding "understanding exceptions" to the list of pre-requisites for learning/using php would just be a poor decision looking at what made php as popular as it is today.

the solution there would not be to "fix" ctor, but instead to use a factory, and have constructors without logic. the other case where one can't live without exceptions is when using overloads (solution: don't use overloads when you care about signaling error conditions).


couldn't agree more btw, i think exceptions are cool, and certainly have their usages, but after reading the comments on this thread, i think we are about to hand out cocaine to children. hopefully one hit is enough ;-)

either way its rc2, not much can be done now...

-sterling

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to