# [PATCH] in_class_exists() for detecting __autoload() called by class_exists() and interface_exists()

- Thread: https://externals.io/message/32585
- Original archive: https://news-web.php.net/php.internals/32585
- Started by Gregory Beaver on 2007-10-03T06:21:50+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32585 — Gregory Beaver — 2007-10-03T06:21:50+00:00

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

Hi again,

The attached patches for PHP 5.3 and PHP 6 add in_class_exists().  The
function is intended to be used in __autoload().  The following test
script demonstrates its usage:

<?php
var_dump(in_class_exists('test'));
function __autoload($class)
{
    var_dump(in_class_exists($class));
    eval ('class ' . $class . ' {}');
}

$a = new Bungalow; // autoload called, not because of `class_exists()`,
bool(false)
class_exists('test'); // autoload called because of `class_exists()`,
bool(true)
class_exists('test'); // class exists, no autoload, no output
$a = new test; // autoload not called, no output
$a = new Another; // autoload called, not because of `class_exists()`,
bool(false)
?>

outputting:

bool(false)
bool(false)
bool(true)
bool(false)

The purpose is to allow an autoload handler to detect whether it is safe
to use `die()` or some other method of terminating execution.  A user
calling `class_exists()` clearly expects a return from autoload, whereas a
user instantiating an object will expect a fatal error if the class
cannot be found.

This is useful for PEAR2 as a means of displaying helpful information on
locating missing class files only in cases where a fatal error would
result anyways.

Thanks,
Greg

---

## #32621 — Stanislav Malyshev — 2007-10-03T21:54:02+00:00

URL: https://externals.io/message/32585#32621 · In reply to: #32585

> to use `die()` or some other method of terminating execution.  A user
> calling `class_exists()` clearly expects a return from autoload, whereas a
> user instantiating an object will expect a fatal error if the class
> cannot be found.

This is true, but __autoload should never do it, the engine should do 
it. So I think it's better just not make autoloader *ever* produce 
errors. Can't find it - just give up, if engine really needs it it'd 
bail out :)

> This is useful for PEAR2 as a means of displaying helpful information on
> locating missing class files only in cases where a fatal error would
> result anyways.

Well, autoloader might print debug traces etc. if the developer wants 
it, but I don't see why this should be unique feature for class_exists.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #32623 — Gregory Beaver — 2007-10-03T23:17:22+00:00

URL: https://externals.io/message/32585#32623 · In reply to: #32621

Stanislav Malyshev wrote:
> > […]
>
> This is true, but __autoload should never do it, the engine should do
> it. So I think it's better just not make autoloader *ever* produce
> errors. Can't find it - just give up, if engine really needs it it'd
> bail out :)
Another possibility: could there be a way to add context information (a
string) to the error message the engine displays?  This would alleviate
my concern.
> > […]
>
> Well, autoloader might print debug traces etc. if the developer wants
> it, but I don't see why this should be unique feature for class_exists.

the point is that if the user is using `class_exists()` they don't want
extra output on missing class.

Greg

---

## #32626 — Stanislav Malyshev — 2007-10-03T23:44:25+00:00

URL: https://externals.io/message/32585#32626 · In reply to: #32623

> Another possibility: could there be a way to add context information (a
> string) to the error message the engine displays?  This would alleviate
> my concern.

Why not just log it into some logfile? Remember that autoloaders can be 
chained, so engine would have to assemble all strings, display them in 
order, etc. Why create another mini-logging system?

> the point is that if the user is using `class_exists()` they don't want
> extra output on missing class.

But how would you know the class is missing? Maybe next loader in chain 
would find it. Anyway, this looks like debug feature, why shouldn't it 
be handled on application level?
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #32631 — Benjamin Schulz — 2007-10-04T14:34:14+00:00

URL: https://externals.io/message/32585#32631 · In reply to: #32585

Hi,
What about __class_exists() and SPL_register_class_exists_handler()?  
One could suppose the classloader (the one that implements  
__autoload) knows if a class exists (as in: is registered within the  
classloader or the class-file exists) without loading it and maybe  
the class that calls `class_exists()` never uses it.

regards,
Benjamin
