At 07:36 AM 4/15/2004 +0200, 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.

In order to use them wisely you need to be an advanced programmer. Personally, I must say that when exceptions are used correctly (and some of the comments on this list make me think that some of the people here used them incorrectly), then I think they are extremely useful.
I don't think allowing the return of anything except for the object itself is *fixing* PHP.
Anyway, you don't have to put logic in the constructor.
I am fine with not forcing people to use exceptions. However, I am also fine that some of the OO extensions who wish to use exceptions use them. For example, in the SOAP extension it is very useful that in the SoapServer you can throw an exception and that'll create a SOAP error. Any other value you return will be the SOAP result. This is a perfect place for exceptions.



Andi


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



Reply via email to