Re: class_is_loadable?

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

 



Hi,

The problem is not the autoload but the implementation of such function.
class_is_loadable mean, "hey php look at my class somewhere in my files". PHP should inspect some files, in some directories and list classes.
Which files, which extensions files, in which directories ? ...
In my mind you must replan your autoload, for exemple make a link beetween classes and files name, ie : if file_exists( A.class.php ) include_once( B.class.php) else include_once( A.class.php ); Check the factory method at Zend site, that explain how to work with class method, without to know the exact name of class (ex : load an specific class depending of the database available)



"Larry Garfield" <larry@xxxxxxxxxxxxxxxx> a écrit dans le message de news:200807051236.13458.larry@xxxxxxxxxxxxxxxxxxx
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


[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