Req #49618 [Com]: spl_autoload_register destroys __autoload hook

From: Date: Thu, 07 Jul 2011 21:54:34 +0000
Subject: Req #49618 [Com]: spl_autoload_register destroys __autoload hook
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161697@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49618&edit=1 ID: 49618 Comment by: staff at pro-unreal dot de Reported by: jost dot boekemeier at googlemail dot com Summary: spl_autoload_register destroys __autoload hook Status: Open Type: Feature/Change Request Package: SPL related Operating System: * PHP Version: 5.2.11 Block user comment: N Private report: N New Comment: How about that (php 5.3): <?php spl_autload_register(function($class) { if (function_exists('__autoload') __autoload($class); }) Previous Comments: ------------------------------------------------------------------------ [2009-11-19 11:22:34] colder@php.net There is no way to "fix" this in PHP. __autoload in itself is not deprecated, but it is not a good idea to use it as part of a library, for obvious reasons. However, if you know your code will only have one autoload function, using __autoload will be just fine. To me it seems that the library writers that still use __autoload are at fault here. ------------------------------------------------------------------------ [2009-09-23 08:53:11] jost dot boekemeier at googlemail dot com Right. In particular: <?php // pjb library function autoload_pjb($x) {echo "pjb "; return false;} spl_autoload_register("autoload_pjb"); if(function_exists("__autoload")) spl_autoload_register("__autoload"); ?> <?php // joomla library function __autoload($x) {echo "joomla "; return false;} ?> <?php // application require("pjb.php"); require("joomla.php"); @new Foo(); ?> Please see the link posted with the bug report for all the details. However, I think neither the user nor joomla nor I have to invent strange workarounds for bugs in the php implementation. Any new introduced PHP feature must be compatible with earlier PHP features, unless the earlier features have been deprecated and a clear migration path exists. The old __autoload is neither deprecated nor is spl_autoload_register compatible with it. Whoever wrote it and the comment "....This is because spl_autoload_register() will effectively replace the engine cache fo the __autoload function by either spl_autoload() or spl_autoload_call(). " wrote a bug. > Thus, calling spl_autoload_register() replaces __autoload() If spl_autoload_register is intended to replace the old __autoload machinery, the old __autoload should have been deprecated and a clear migration path should have been given to those using it. ------------------------------------------------------------------------ [2009-09-23 07:23:07] sjoerd@php.net So the problem occurs with: include('spl_autoload_register.php'); // uses spl_autoload_register include('autoload.php'); // defines __autoload ------------------------------------------------------------------------ [2009-09-22 17:39:32] jost dot boekemeier at googlemail dot com > if (function_exists("__autoload")) spl_autoload_register("__ autoload") Doesn't work. In line 7 __autoload is not yet defined. Note that every line in the given example comes from a separate include()d php library. This is a link-time problem which only php can resolve: it must save a legacy __autoload handler before creating the spl_autoload stack and call the legacy autoload after all other registered spl_autoload hooks. ------------------------------------------------------------------------ [2009-09-22 17:26:38] sjoerd@php.net If two or more of the libraries use __autoload, it won't work, and there is nothing anybody can do about it. If all of the libraries use spl_autoload_register, it works as it should. If one of the libraries uses __autoload and others use spl_autoload_register, it is the task of the library that uses spl_autoload_register to register __autoload as well, like this: if (function_exists("__autoload")) spl_autoload_register("__autoload"); ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=49618 -- Edit this bug report at https://bugs.php.net/bug.php?id=49618&edit=1

« previous php.bugs (#161697) next »