Anything will. Calling a static method in the class, accessing a class constant, extending it etc.dreamscape wrote:new is not the only autoload trigger. Functions such as class_exists and interface_exists will also trigger it.arborint wrote:The are simply error callback functions for the new keyword.
Question about autoloading
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Yes I know, and to my mind it only makes things worse. Now those functions are file loaders that always return true if there is an autoload registered, otherwise they work as named. Admittedly they added a flag, but it seems like the default should be false. I guess they are now autoload checkers ... ok. So now we can do the same check we did before to make sure the autoload worked?!? If they keep slapping on features then eventually everyone with either be happy or used to it.dreamscape wrote:new is not the only autoload trigger. Functions such as class_exists and interface_exists will also trigger it.
(#10850)
- dreamscape
- Forum Commoner
- Posts: 87
- Joined: Wed Jun 08, 2005 10:06 am
- Contact:
Not sure what you mean... if there is an autoload function, they still return false if the class does not exist. They just give the autoloader a chance to load the class if it has not been loaded yet. If the class does not exist and no autoloader loads it, they still return false. So I really don't know where you get your information from that they "always return true," because it is completely incorrect.arborint wrote:Now those functions are file loaders that always return true if there is an autoload registered
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Here is example code:dreamscape wrote:So I really don't know where you get your information from that they "always return true," because it is completely incorrect.
Foo.php
Code: Select all
<?php
class Foo {
function Foo() {
echo 'Foo';
}
}Code: Select all
<?php
function __autoload($class) {
include $class . '.php';
}
if (class_exists('Foo')) {
echo '1st check Foo exists.<br/>';
}
if (class_exists('Foo')) {
echo '2nd check Foo exists.<br/>';
}Code: Select all
1st check Foo exists.
2nd check Foo exists.I think you get these kind of patchy, reactive design decisions when you don't really appreciate or understand the problem. They tacked on __autoload(), then fixed its problems with SPL, then fixed more problems with class_exists() and interface_exists(), and there will be more. Hopefully they are learning as they go. It just seems like a register_globals type solution to me.
(#10850)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Hmm... I see what you're saying and I do agree, but at the same time, the class does exist if PHP can find it. It's like in Java, you don't have to "import" everything you need, if you call org.yourpackage.SomeClass it simply finds that file from the directory layout/classpath. Actaully, that's probably a bad analogy. class_exists() should IMHO, return true if the class exists, even if it's not currently included. Now if get_declared_classes() invoked to autoloader I'd be concerned.
I personally use autoloaders for convenience. I can see both sides of the argument however.
I personally use autoloaders for convenience. I can see both sides of the argument however.
- dreamscape
- Forum Commoner
- Posts: 87
- Joined: Wed Jun 08, 2005 10:06 am
- Contact:
Well, I completely disagree that class_exists() should not invoke autoload by default.
If your application has an autoload function, and the function can load the class, then to the application, the class does in fact exist. With autoload, just because a class is not "loaded," doesn't mean it does not exist. The functions are name *_exists(), not *_loaded().
And anyways, if you don't agree with them invoking the autoloader, you can always supply the optional boolean parameter false to stop them from doing so.
If your application has an autoload function, and the function can load the class, then to the application, the class does in fact exist. With autoload, just because a class is not "loaded," doesn't mean it does not exist. The functions are name *_exists(), not *_loaded().
And anyways, if you don't agree with them invoking the autoloader, you can always supply the optional boolean parameter false to stop them from doing so.
I disagree. *_exists()... where? I see it as "exists in the current script" - if it's not loaded, it doesn't exists in the script.dreamscape wrote:The functions are name *_exists(), not *_loaded().
I'll go a bit further to emphasize this. file_exsits() for example... do you want it to search on google too and return true because the file exists on another server? This does not make sense. I know I went too far, but it was just for the example
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I don't expect file_exists() to only return true if the file has been included in my PHP script. It's a totally different function, with a different purposeOren wrote:I disagree. *_exists()... where? I see it as "exists in the current script" - if it's not loaded, it doesn't exists in the script.dreamscape wrote:The functions are name *_exists(), not *_loaded().
I'll go a bit further to emphasize this. file_exsits() for example... do you want it to search on google too and return true because the file exists on another server? This does not make sense. I know I went too far, but it was just for the example. Do you expect file_exists() to search in your whole server for the file you pass to it?
I think the point of class_exists() is to make an informed decision about whether or not you are safe to do something involving the use of the class. IMHO, you needn't care about the implementation of how or why the class exists, you just need to know it can be used.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
OK ... that's a perfectly valid opinion ... my point was that the current design is one of several possible designs. I think others might be better and you might like them as well.dreamscape wrote:Well, I completely disagree that class_exists() should not invoke autoload by default.
So now we can do the the following:dreamscape wrote:If your application has an autoload function, and the function can load the class, then to the application, the class does in fact exist. With autoload, just because a class is not "loaded," doesn't mean it does not exist. The functions are name *_exists(), not *_loaded().
And anyways, if you don't agree with them invoking the autoloader, you can always supply the optional boolean parameter false to stop them from doing so.
Code: Select all
if (! class_exists('Foo', false)) { // function as class checker
class_exists('Foo', true); // same function as class loader
}(#10850)
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Well as dreamscape rightly points out once you go the autoload route any class that can be autoloaded must be treated as loaded because it automatically will be. So it makes sense that the behavior will trickle into many things related to classes. And as with many other parts of PHP, once we figure out how it works, we can make workable solutions.Oren wrote:Agree. I really don't see why class_exists() should trigger the __autoload() function, but if that's how they decided to go, at least make it defaults to false.
(#10850)