|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-22 05:24 UTC] jost dot boekemeier at googlemail dot com
Description: ------------ The first call to spl_autoload_register destroys an existing __autoload hook. Please either deprecate __autoload and/or register __autoload with spl_autoload. Complete problem description is here: http://sourceforge.net/mailarchive/forum.php? thread_name=3afa16cf0909210312v3e102491n18701bcca0f5e030%40mail.gmail.com &forum_name=php-java-bridge-users Reproduce code: --------------- <?php function autoload_legacy($x) {echo "legacy "; return false;} function autoload_spl1($x) {echo "spl1 "; return false;} function autoload_spl2($x) {echo "spl2 "; return false;} function autoload_spl3($x) {echo "spl3 "; return false;} spl_autoload_register("autoload_spl1"); function __autoload($x) {return autoload_legacy($x);} spl_autoload_register("autoload_spl2"); spl_autoload_register("autoload_spl3"); @new Foo(); ?> Expected result: ---------------- spl1 spl2 spl3 legacy Actual result: -------------- spl1 spl2 spl3 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 10:00:01 2025 UTC |
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");> 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.So the problem occurs with: include('spl_autoload_register.php'); // uses spl_autoload_register include('autoload.php'); // defines __autoloadRight. 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.How about that (php 5.3): <?php spl_autload_register(function($class) { if (function_exists('__autoload') __autoload($class); })