# class_exists and __autoload

- Thread: https://externals.io/message/16969
- Original archive: https://news-web.php.net/php.internals/16969
- Started by James Crumpton on 2005-06-28T21:32:03+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #16969 — James Crumpton — 2005-06-28T21:32:03+00:00

URL: https://externals.io/message/16969#16969

I opened a bug (http://bugs.php.net/bug.php?id=33459) to report an issue with
the default behavior of class_exists calling __autoload in PHP5. The bug was
marked as bogus, and basically stated that it's PEARs problem. After responding,
the bug can now longer be searched for.

I believe this still requires some attention as the issues around it are very
similar in nature to the ones with instanceOf and __autoload. (See
http://news.php.net/php.internals/16746)

Basically, adding __autoload to any script using
Calendar/DB_DataObject/SOAP/Validate (just to name a few) will throw a fatal error.

Example being:

<?php
require_once('Calendar/Calendar.php');

$cal = new Calendar();

function __autoload($name) {
         require_once($name);
}
?>

This isn't specific to PEAR however... any script/class that uses
!class_exists() to load a file not in 'include_path' will fail when __autoload
is used in conjunction.

Furthermore, I fail to see why such a "feature" is in place when it makes all
existing if(!class_exists()) {} constructs worthless.

if (!class_exists($className)) {
	// ... do something interesting ...
	// well, with autoload defined, this block will never be reached.
	// autoload will either load the class and the above condition will
	// be false, or autoload will throw a fatal error and we never get here
}

I just don't understand the logic behind throwing a fatal error when I'm writing
code to try and prevent one from occurring.

I realize a second parameter is available to class_exists that can be set to
false, which will prevent __autoload from being called, but developers don't
always have the luxury of modifying other peoples code to make it work they way
they intended it (such is the case with PEAR classes).

If someone really wants this behavior (though I can't understand for life of me
why), then allow them to pass `TRUE` as the second parameter, but leave the
default be false. Or at the very least let autoload return false, so that the
rest of the script can do it's thing... or maybe allow for a php.ini option to
set the default behavior to false (though this option can make some scripts less
portable).

-james

---

## #16970 — Johannes Schlueter — 2005-06-28T22:01:16+00:00

URL: https://externals.io/message/16969#16970 · In reply to: #16969

Hi James,


> function __autoload($name) {
>          require_once($name);
> }

> I just don't understand the logic behind throwing a fatal error when I'm
> writing code to try and prevent one from occurring.

You should add error checking to your autoload function. Either by some 
 `file_exists()`/is_readable()  check or by using include instead of require 
(maybe even with suppressing errors if you dislike `is_readable()`...)

For more please ask on the generals list or some other support forum.

johannes

---

## #16975 — Alan Knowles — 2005-06-28T23:22:04+00:00

URL: https://externals.io/message/16969#16975 · In reply to: #16969

<rant>
This yet again illustrates the sillyness of autoload, if we are going to
have a feature that autoloads classes it should be explicit in it's
usage:

autoload("Class_Name");

defaults to loading Class_Name.php ?
if __autoload exists, it calls that..

adding all sorts of magic to the Class operators is digging a hole that
you will be continually answering...
</rant>

Ok - got up the wrong side of bed this morning ;)
Regards
Alan






> I opened a bug (http://bugs.php.net/bug.php?id=33459) to report an issue with
> the default behavior of class_exists calling __autoload in PHP5. The bug was
> marked as bogus, and basically stated that it's PEARs problem. After responding,
> the bug can now longer be searched for.
> 
> I believe this still requires some attention as the issues around it are very
> similar in nature to the ones with instanceOf and __autoload. (See
> http://news.php.net/php.internals/16746)
> 
> Basically, adding __autoload to any script using
> Calendar/DB_DataObject/SOAP/Validate (just to name a few) will throw a fatal error.
> 
> Example being:
> 
> <?php
> require_once('Calendar/Calendar.php');
> 
> $cal = new Calendar();
> 
> function __autoload($name) {
>          require_once($name);
> }
> ?>
> 
> This isn't specific to PEAR however... any script/class that uses
> !class_exists() to load a file not in 'include_path' will fail when __autoload
> is used in conjunction.
> 
> Furthermore, I fail to see why such a "feature" is in place when it makes all
> existing if(!class_exists()) {} constructs worthless.
> 
> if (!class_exists($className)) {
> 	// ... do something interesting ...
> 	// well, with autoload defined, this block will never be reached.
> 	// autoload will either load the class and the above condition will
> 	// be false, or autoload will throw a fatal error and we never get here
> }
> 
> I just don't understand the logic behind throwing a fatal error when I'm writing
> code to try and prevent one from occurring.
> 
> I realize a second parameter is available to class_exists that can be set to
> false, which will prevent __autoload from being called, but developers don't
> always have the luxury of modifying other peoples code to make it work they way
> they intended it (such is the case with PEAR classes).
> 
> If someone really wants this behavior (though I can't understand for life of me
> why), then allow them to pass `TRUE` as the second parameter, but leave the
> default be false. Or at the very least let autoload return false, so that the
> rest of the script can do it's thing... or maybe allow for a php.ini option to
> set the default behavior to false (though this option can make some scripts less
> portable).
> 
> -james
