Re: Class constructor behaviour
Hi François,
> On 17 Jan 2015, at 15:37, François Laupretre <[email protected]> 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. 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.
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/
Thread (19 messages)