|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
class_is_loadable?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@... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: class_is_loadable?First of all, I don't think using autoload is recommended.
If you really want to do it that way, you could check if the file which autoload will try to include exists and if it does, include it and create your object. Actually to make the script more safe, after including the file you should check if the class exists (because the file might exist but not have the class in it) and only then you can make the object of that class. I suppose you could throw the exception you want and do it with the try-catch as you like it :) Sorry if my English is bad and if didn't get what you want to do. "Larry Garfield" <larry@...> wrote in message news:200807051236.13458.larry@...... > 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@... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Re: class_is_loadable?On Saturday 05 July 2008 2:25:20 pm Pulni4kiya wrote:
> First of all, I don't think using autoload is recommended. Why not? I know there is a performance hit for the lookup time, but for the system I'm working on I have already solved that issue with selective pre-caching. > If you really want to do it that way, you could check if the file which > autoload will try to include exists and if it does, include it and create > your object. Well yes, but then I'm not using autoload at all. I may as well just include_once() and class_exists(), which is what I'm trying to avoid, both for code cleanliness and performance (include_once() has its own performance issues). > Actually to make the script more safe, after including the file you should > check if the class exists (because the file might exist but not have the > class in it) and only then you can make the object of that class. > > I suppose you could throw the exception you want and do it with the > try-catch as you like it :) Again, that's basically just reimplementing autoloading in user-space, which defeats the purpose. If I know exactly which autoload routine would be responsible for loading a given class, can I do the check in that autoload routine and throw an exception there? Or would that make insanity happen? :-) -- Larry Garfield larry@... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Re: class_is_loadable?Well reimplementing autoloading doesn't seem such a bad idea.
With the integrated autoload ...there is one very stupid way of doing what you want. Something like this (I suppose you know which class is the parent of the one that is 'missing'): eval("class $class_name extends THE_PARENT {}"); You can put a field with the actual class name and fill it in the constructor so you would know if it's the actual class B or just A with a different name. (What I just wrote looks very stupid... Don't laugh at me very much please. :D) I'll think of something smarter...this is the first thing that came into my mind. Btw why is it so important to use autoloading anyway? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Re: class_is_loadable?Pulni4kiya wrote:
> Well reimplementing autoloading doesn't seem such a bad idea. > With the integrated autoload ...there is one very stupid way of doing > what you want. Something like this (I suppose you know which class is > the parent of the one that is 'missing'): > eval("class $class_name extends THE_PARENT {}"); > You can put a field with the actual class name and fill it in the > constructor so you would know if it's the actual class B or just A > with a different name. > > (What I just wrote looks very stupid... Don't laugh at me very much > please. :D) > > I'll think of something smarter...this is the first thing that came > into my mind. > > Btw why is it so important to use autoloading anyway? > Has anybody used the PECL extension automap yet and if so, are there issues with using that to autoload or not? Greetings, Aschwin Wesselius -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: class_is_loadable?On Sat, Jul 5, 2008 at 1:36 PM, Larry Garfield <larry@...> 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@... > > -- > 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 |
|
|
Re: class_is_loadable?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@...> a écrit dans le message de news:200807051236.13458.larry@...... > 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@... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Re: class_is_loadable?On Sun, Jul 6, 2008 at 6:06 AM, Fabrice VIGNALS <fabrice@...> wrote:
> 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) > file_exist isn't going to help you if the file is in the include path somewhere else. I routinely use the include path to have my shared code base across multiple sites without duplicated files. You'd end up writing some horrible fopen with the use include path flag to test this. var_dump(file_exists('PEAR.php')); <- false var_dump(fopen('PEAR.php', 'r', true)); <- resource if exists -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
| Free Forum Powered by Nabble | Forum Help |