Quantcast

ZF2 Autoloading strategies proposal

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Autoloading strategies proposal

weierophinney
Administrator
-- Greg <[hidden email]> wrote
(on Sunday, 05 September 2010, 04:19 PM -0500):
> I was looking at the Zend_Loader loadClass method and noticed that it
> always throw an exception if it could not load the desired class. Can
> this be optional? Sometimes it might be OK that the class does not
> exist even when trying to load it via the autoloader, eg:

Please look at the implementations in the proposal; none of them utilize
Zend_Loader::loadClass(), and none of them throw exceptions.

Within ZF1's autoloader, we wrap the loadClass() method in a try/catch
block, which allows you to drop to another autoloader if it is unable to
locate the class.

At this point, I'm considering removing Zend_Loader from ZF2 -- with
autoloading as the default, loadClass() really isn't all that necessary,
and most of the other methods present in the class are superceded by
functionality native in PHP 5.3.

> if (class_exists('Custom_Override_Class')) {
>  $obj = new Custom_Override_Class();
> } else {
>  $obj = new Default_Class();
> }
>
> $obj->doSomething();

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 Autoloading strategies proposal

Dolf Schimmel
In reply to this post by Tommy Smith
Which requires an exception to be instantiated, whereas when you use
that function merely to check if a class is loadable, an unloadable
file isn't an unexpected situation, so doesn't need an exception.
Therefore I can imagine it makes sense if there's a method like
isLoadable() which simply returns a boolean, which is used by the
method that actually loads the class.

Dolf
-- Freeaqingme

On Mon, Sep 6, 2010 at 12:26 AM, Tommy Smith <[hidden email]> wrote:

> Instead of making it optional, you probably just need to catch the exception
> and handle it in a way that doesn't make your code blow up :-)
>
>
> On Sun, Sep 5, 2010 at 3:19 PM, Greg <[hidden email]> wrote:
>>
>> I was looking at the Zend_Loader loadClass method and noticed that it
>> always throw an exception if it could not load the desired class. Can
>> this be optional? Sometimes it might be OK that the class does not
>> exist even when trying to load it via the autoloader, eg:
>>
>>
>> if (class_exists('Custom_Override_Class')) {
>>  $obj = new Custom_Override_Class();
>> } else {
>>  $obj = new Default_Class();
>> }
>>
>> $obj->doSomething();
>>
>> --
>> Greg
>
>
12
Loading...