class_is_loadable?

This is a discussion on class_is_loadable? within the PHP General forums, part of the PHP Programming Forums category; Greetings, all. I am trying to figure out a way to implement the following logic, but I am not sure ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-05-2008
Larry Garfield
 
Posts: n/a
Default 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 (ClassDoesntExistEvenAfterRunningThroughAutoloadEx ception $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@garfieldtech.com
Reply With Quote
  #2 (permalink)  
Old 07-05-2008
Pulni4kiya
 
Posts: n/a
Default 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@garfieldtech.com> wrote in message
news:200807051236.13458.larry@garfieldtech.com...
> 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 (ClassDoesntExistEvenAfterRunningThroughAutoloadEx ception $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@garfieldtech.com


Reply With Quote
  #3 (permalink)  
Old 07-05-2008
Larry Garfield
 
Posts: n/a
Default Re: [PHP] 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@garfieldtech.com
Reply With Quote
  #4 (permalink)  
Old 07-05-2008
Pulni4kiya
 
Posts: n/a
Default Re: [PHP] 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?

Reply With Quote
  #5 (permalink)  
Old 07-05-2008
Aschwin Wesselius
 
Posts: n/a
Default Re: [PHP] 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?
>

Hi,

Has anybody used the PECL extension automap yet and if so, are there
issues with using that to autoload or not?

Greetings,

Aschwin Wesselius
Reply With Quote
  #6 (permalink)  
Old 07-06-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] class_is_loadable?

On Sat, Jul 5, 2008 at 1:36 PM, Larry Garfield <larry@garfieldtech.com> 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 (ClassDoesntExistEvenAfterRunningThroughAutoloadEx ception $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@garfieldtech.com
>
> --
> 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 }
Reply With Quote
  #7 (permalink)  
Old 07-06-2008
Fabrice VIGNALS
 
Posts: n/a
Default 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@garfieldtech.com> a écrit dans le message de
news:200807051236.13458.larry@garfieldtech.com...
> 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 (ClassDoesntExistEvenAfterRunningThroughAutoloadEx ception $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@garfieldtech.com


Reply With Quote
  #8 (permalink)  
Old 07-07-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] Re: class_is_loadable?

On Sun, Jul 6, 2008 at 6:06 AM, Fabrice VIGNALS <fabrice@vignals.com> 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
Reply With Quote
  #9 (permalink)  
Old 07-07-2008
mrclay
 
Posts: n/a
Default Re: class_is_loadable?

> 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


No, in PHP5, class_exists('B') will try to autoload B before
determining its existence, so, if it returns false, it really *is*
unloadable. So your code should already do what you want:

if (class_exists('B')) {
$foo = new B();
} else {
$foo = new A();
}

Steve
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:17 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0