Page 3 of 5
Posted: Sat May 12, 2007 9:54 am
by Chris Corbyn
dreamscape wrote:arborint wrote:The are simply error callback functions for the new keyword.
new is not the only autoload trigger. Functions such as
class_exists and
interface_exists will also trigger it.
Anything will. Calling a static method in the class, accessing a class constant, extending it etc.
Posted: Sat May 12, 2007 3:12 pm
by Christopher
dreamscape wrote:new is not the only autoload trigger. Functions such as class_exists and interface_exists will also trigger it.
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.
Posted: Sat May 12, 2007 7:55 pm
by dreamscape
arborint wrote:Now those functions are file loaders that always return true if there is an autoload registered
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.
Posted: Sun May 13, 2007 12:47 am
by Christopher
dreamscape wrote:So I really don't know where you get your information from that they "always return true," because it is completely incorrect.
Here is example code:
Foo.php
Code: Select all
<?php
class Foo {
function Foo() {
echo 'Foo';
}
}
test.php
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/>';
}
It will output:
Code: Select all
1st check Foo exists.
2nd check Foo exists.
That means if an autoload function is present that class_exists() returns true if a class is not loaded -- and load the class, and of course true if the class has been previously loaded. As both I and you note, class_exists() has now been turned into autoload_succeeded() check. I didn't say that this did not make some kind of sense and might not be useful, but you have now turned class_exists() into a file loader and autoload success checker.
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.
Posted: Sun May 13, 2007 6:53 am
by Chris Corbyn
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.
Posted: Sun May 13, 2007 9:10 am
by dreamscape
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.
Posted: Sun May 13, 2007 9:40 am
by Oren
dreamscape wrote:The functions are name *_exists(), not *_loaded().
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.
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?
Posted: Sun May 13, 2007 11:25 am
by Chris Corbyn
Oren wrote:dreamscape wrote:The functions are name *_exists(), not *_loaded().
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.
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 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 purpose
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.
Posted: Sun May 13, 2007 11:29 am
by Oren
d11wtq wrote: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 purpose

No, it's not what I meant. I don't expect it to return true if the file was included in the script

Posted: Sun May 13, 2007 12:30 pm
by Christopher
dreamscape wrote:Well, I completely disagree that class_exists() should not invoke autoload by default.
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: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.
So now we can do the the following:
Code: Select all
if (! class_exists('Foo', false)) { // function as class checker
class_exists('Foo', true); // same function as class loader
}
And as I have said repeatedly, it is not that I find that the current design is not workable. It is that I don't find the current design very good. They are fumbling their way to an almost complete solution.
Posted: Sun May 13, 2007 1:05 pm
by Ambush Commander
While I won't pass judgment on the new behavior, I have certainly been bitten by the autoloading behavior of class_exists.
Posted: Sun May 13, 2007 1:11 pm
by Chris Corbyn
Ambush Commander wrote:While I won't pass judgment on the new behavior, I have certainly been bitten by the autoloading behavior of class_exists.
So have I

Posted: Sun May 13, 2007 1:11 pm
by John Cartwright
I'm with you arborint, the one thing that really gets me is the default value for class_exists is to use an autoloader. If the second parameter by default was false, then I would be less inclined to disagree with the current design.
Posted: Sun May 13, 2007 1:19 pm
by Oren
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.
Posted: Sun May 13, 2007 2:54 pm
by Christopher
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.
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.