php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54541 spl_autoload_register Loading order
Submitted: 2011-04-15 15:03 UTC Modified: 2011-04-17 05:26 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: gys0324 at yeah dot net Assigned:
Status: Open Package: SPL related
PHP Version: 5.3.6 OS: Win
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-04-15 15:03 UTC] gys0324 at yeah dot net
Description:
------------
spl_autoload_register

Loading order 

Test script:
---------------
A0Class.php
<?
namespace TESTING;

use TESTING\BClass;
class A0Class extend BClass{

}

A1Class.php
<?
namespace TESTING;

use TESTING\BClass;
class A1Class extend BClass{
}

BClass.php
<?
namespace TESTING;

use TESTING\BClass;
class BClass{

     public function __construct(){
         echo "Child Start";
     }
}

Bootstrap.php
<?php
namespace TESTING;

class Bootstrap {
	
	private static $classes = array ();
	
	public static function init() {
		
		Bootstrap::requireOnce ( __DIR__ );
		
		Bootstrap::loadClass ();
	}
	
	public static function getClasses() {
		
		return Bootstrap::$classes;
	}
	
	private static function requireOnce($path) {
		
		foreach ( scandir ( $path ) as $object ) {
			
			if ($object != '.' && $object != '..') {
				
				$object = $path . DIRECTORY_SEPARATOR . $object;
				
				if (is_file ( $object ) && file_exists ( $object )) {
					if (preg_match ( '/\.php$/', $object ) && ! preg_match ( '/Bootstrap\.php$/', $object ) && 
! preg_match ( '/testFile\.php$/', $object )
&& !in_array()) {
						Bootstrap::$classes [] = $object;
					}
				
				} else if (is_dir ( $object )) {
					
					Bootstrap::requireOnce ( $object );
				
				}
			}
		}
	}
	
	private static function loadClass() {
		
		for($i = 0; $i < count ( Bootstrap::$classes ); $i ++) {
			
			require_once Bootstrap::$classes [$i];
		}
	}
}

spl_autoload_register ( array ('TESTING\Bootstrap', 'init' ) );

testFile.php
<?php

require_once 'lib/Bootstrap.php';

use TESTING\A0Class;

$a0 = new A0Class();



Expected result:
----------------
Child Start

Actual result:
--------------
Fatal error: Class 'TESTING\BClass' not found 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-15 15:44 UTC] gys0324 at yeah dot net
There is two classes extend BClass,
so the error has been happened.
But if just one class which is A0 or A1 extends BClass,
the expected result will be displayed.
 [2011-04-17 05:26 UTC] gys0324 at yeah dot net
-PHP Version: 5.2.17 +PHP Version: 5.3.6
 [2011-04-17 05:26 UTC] gys0324 at yeah dot net
the problem is always found by the latest version
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC