php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44848 autoload fails with complex loading scheme
Submitted: 2008-04-27 21:22 UTC Modified: 2017-12-06 22:43 UTC
Votes:10
Avg. Score:4.0 ± 1.3
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:4 (80.0%)
From: nicolas dot grekas+php at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nicolas dot grekas+php at gmail dot com
New email:
PHP Version: OS:

 

 [2008-04-27 21:22 UTC] nicolas dot grekas+php at gmail dot com
Description:
------------
Hard to explain, see code... I think that PHP should be able to handle this kind of loading scheme.

Here is what I thought this code would do :
1. __autoload('A') is called
2. inside this call for A:
   2.1 class B is defined, which extends C
   2.2 as C is not defined, __autoload('C') is called
   2.3 inside this call for C:
       2.3.1 class C is defined
       2.3.2 (now we have everything needed for class B, haven't we ?)
       2.3.3 class A extends B
   2.4 we leave the __autoload('C') context
3. we leave the __autoload('A') context

The bug is at step 2.3.3 : "class A extends B" triggers an autoload('B'), which should not occurs, as B should be already defined, thanks to 2.3.2...

Reproduce code:
---------------
<?php

function __autoload($class)
{
        switch ($class)
        {
        case 'A':
        case 'B':
                class B extends C {};
                break;

        case 'C':
                class C {};
                class A extends B {};
                break;

        }
}

echo (int) class_exists('A');


Expected result:
----------------
1

Actual result:
--------------
Fatal error: Cannot redeclare class B in [...] on line 9


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-05 08:33 UTC] jille at hexon dot cx
I tried creating a work-around for this.
However, this didn't work:
So class B is half-existent ?
You can not define it, but it also doesn't exists...

<?php

  function __autoload($class)
  {
          switch ($class)
          {
          case 'A':
          case 'B':
                  if(class_exists('B', false))
                    return;
                  class B extends C {};
                  break;

          case 'C':
                  class C {};
                  class A extends B {};
                  break;

          }
  }

  echo (int) class_exists('A');
?>
 [2009-04-19 23:04 UTC] nicolas dot grekas+php at gmail dot com
I just tried this on the command line :
http://windows.php.net/downloads/snaps/php-5.2-nts-win32-VC6-x86-latest.zip

still doesn't work sorry
 [2009-06-03 08:44 UTC] admin at ifyouwntblood dot de
class B does not exist until __autoload() for C returns!
 [2011-04-08 18:11 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -PHP Version: 5.*, 6CVS (2009-04-19) +PHP Version: 5.*
 [2017-12-06 22:43 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-12-06 22:43 UTC] nikic@php.net
The comment by admin at ifyouwntblood dot de is correct, class B is not going to be defined until the autoload for C finished. As such, everything is working as intended.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 15 01:01:27 2025 UTC