Re: class_is_loadable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Jul 5, 2008 at 1:36 PM, Larry Garfield <larry@xxxxxxxxxxxxxxxx> wrote:
> Greetings, all.
>
> I am trying to figure out a way to implement the following logic, but I am not
> sure if it is possible to do so without a lot of additional side work:
>
> I have a class, A, and another class B that extends A.  They live in separate
> files.  The logic I need to implement is as follows:
>
> if (class_exists('B')) {
>  $foo = new B();
> }
> else {
>  $foo = new A();
> }
>
> That is all well and good if both A and B are already loaded and parsed, but I
> am using spl_autoload to lazy-load classes as needed.  That means the
> class_exists() call will return false if B exists but hasn't been included
> yet.  What I would like to happen is for PHP to include B if it exists or
> give a non-fatal error if it doesn't so that I can instantiate A instead.
>
> Ideally, the logic would be something like the following:
>
> try {
>  $foo = new B();  // Try to autoload B, throw exception if it can't.
> }
> catch (ClassDoesntExistEvenAfterRunningThroughAutoloadException $e) {
>  $foo = new A(); // May autoload A at this point, too.
> }
> // do stuff with $foo
>
> However, as far as I am aware $foo = new B(); will cause a fatal exception if
> autoload doesn't find a B.
>
> Does anyone know of a way to achieve the above effect?  This is specifically
> for PHP 5.2 and later.  Thanks.
>
> --
> Larry Garfield
> larry@xxxxxxxxxxxxxxxx
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Perhaps this might do it:
spl_autoload_call('someclass');
if (!class_exists('someclass', false)) { load class A }

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux