"Andrea Faulds" wrote in message
news:23490588-0131-4b0f-a7aa-c9c8c7666...@ajf.me...
Hi François,
On 17 Jan 2015, at 15:37, François Laupretre <franc...@tekwire.net>
wrote:
I would prefer deciding that returning null is the standard way for a
constructor to inform the PHP core that the object creation failed (for
any reason). This would be trapped by the core and cause a catchable
fatal error.
This is more compatible with the 'exception-less' behavior of the core in
general. The constructor is still free to throw an exception but this
wouldn't be encouraged nor the standard way to inform the core that an
error occurred.
I don’t really agree here. For some reason we have this tradition of not
using exceptions for “procedural” stuff. That sort of makes sense for
functions, but classes are “OOP” and therefore I don’t see a good reason
why they shouldn’t throw exceptions.
You are forgetting one minor thing - an object may be instantiated from a
piece of procedural code. In fact the very first object can only be
instantiated from procedural code. The idea that every piece of code that
uses "new" will have to be amended to be in a try...catch block is a huge
amount of work that does not solve any problem (except to satisfy the
delicate sensibilities of the OO purists).
Exceptions and OOP (and namespaces, even) should not be the domain of
userland exclusively, they are also the domain of internals/core. If you
really hate exceptions, I’d say you should just use one of those ridiculous
“procedural” alternative functions (that are just methods in disguise) that
we went to the bother of adding.
If you really want a clever way of introducing exceptions into PHP without
breaking huge amounts of existing code then how about this idea - change the
language to detect if a function/method is being called within a try...catch
block or not. If it is then throw an exception, but if it is not then use
the old error handler. In this way the use of exceptions would not be forced
on the developer and existing code would not break. This would me more work
for the core developers, but no broken code in userland.
By the way, it irks me (and many others) that PDO doesn’t throw exceptions
by default. It really ought to, it’s an OOP API, I’d love to see that
changed in PHP 7.
Thanks.
--
Andrea Faulds
http://ajf.me/
--
Tony Marston
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php