Edit report at http://bugs.php.net/bug.php?id=53351&edit=1
ID: 53351 Updated by: ka...@php.net Reported by: php at addiks dot de Summary: Autoload should not cancel if called for same class twice -Status: Open +Status: Closed Type: Feature/Change Request Package: SPL related Operating System: Ubuntu 10.04 PHP Version: 5.3.3 -Assigned To: +Assigned To: Kalle Block user comment: N Private report: N New Comment: This isn't really a logically solution to start skipping calls as the compiler has no idea about its previous state. If you have: a.php: class A { } b.php: class B extends A { } and in your main script: function __autoload($class) { require $class . '.php'; } then it will work, because in your current case you are ending up in a recursive call and will either have a fatal error about it or a memory_limit reach (as the Engine will keep running the code until it runs dry of memory) So the proper solution here is to not have such statically code that depends on other stuff in the autoloader, and its not something that any of us are going to implement, sorry :) Previous Comments: ------------------------------------------------------------------------ [2010-11-19 09:46:13] php at addiks dot de Description: ------------ hi, currently i am using a require-all-script in my system that just plain require-once's all files with correct ending (".class.php";".mdl.php";...) it can find. This happens inside of an autoload-method, calling itself again when a depency (e.g. inheritance) occours. when called again, it will skip all already called includes, since they are require-_once_. Whith this loader i can just add a class file and it will be loaded automaticly without needing me to register it somewhere manually. But it seems i have misunderstood the documentation, because i now have noticed that when two classes extends the same class, it will fail at the second child-class complaining that the parent-class is missing instead of just calling autoload again for the same class, which i thought will happen. now i have to build the initial-loader much more complex, holding a cached depency-list of classes or looking into the source-code when called. Test script: --------------- <?php function __autoload($class){ // throws "Class C not found" // i think it would be cool if it would call autoload again to load C, // so this require_once wont be called again, class C would be loaded and everything would be fine. require_once("B.class.php"); // <-- "<?php class B extends C{}" // this will never be called require_once("C.class.php"); // <-- "<?php class C{}" } // this will call autoload to load class "C" require_once("A.class.php"); // <-- "<?php class A extends C{}" echo "this will never be echo'd..."; ?> Expected result: ---------------- <?php // EXPECTED: function __autoload($class){ // calls autoload again, will be skipped in that second call require_once("B.class.php"); // <-- "<?php class B extends C{}" // will be included in second call, then skippen when back in first run require_once("C.class.php"); // <-- "<?php class C{}" } // this will call autoload to load class "C" require_once("A.class.php"); // <-- "<?php class A extends C{}" // C, B and A are loaded correctly ?> Actual result: -------------- <?php // ACTUAL: function __autoload($class){ // throws fatal error "Class C not found" require_once("B.class.php"); // <-- "<?php class B extends C{}" // this will never be called require_once("C.class.php"); // <-- "<?php class C{}" } // this will call autoload to load class "C" require_once("A.class.php"); // <-- "<?php class A extends C{}" // none class clould be loaded, since it Fatal-error's at B. ?> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53351&edit=1