php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65999 autoloading of aliased namespaced classes fails when including twice a file
Submitted: 2013-10-30 10:49 UTC Modified: 2013-10-31 16:11 UTC
From: giunta dot gaetano at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.21 OS: win7 64bit
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: giunta dot gaetano at gmail dot com
New email:
PHP Version: OS:

 

 [2013-10-30 10:49 UTC] giunta dot gaetano at gmail dot com
Description:
------------
Imagine the following situation:
- file X.php has a USE statement aliasing a class name A\B\C as D
- it includes other files, which set up autoloading, then require X.php again
- when X.php is reincluded, it executes some code, using an object: $x = new D()

In this case, the functions registered for class autoloading will get passed the string 'D' instead of 'A\B\C'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-30 10:50 UTC] giunta dot gaetano at gmail dot com
-Summary: autoloading of alaised namespaced classes fails when including twice a file +Summary: autoloading of aliased namespaced classes fails when including twice a file
 [2013-10-30 10:50 UTC] giunta dot gaetano at gmail dot com
[edited bug name]
 [2013-10-30 14:48 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2013-10-30 14:48 UTC] nikic@php.net
Could you please provide a short reproduce script for your issue? I tried the following code, which autoloaded 'A\B\C', not 'D':

<?php
use A\B\C as D;

if (!isset($alreadyIncluded)) {
	spl_autoload_register('var_dump');
	$alreadyIncluded = true;
	include __FILE__;
} else {
	new D;
}
 [2013-10-30 16:37 UTC] giunta dot gaetano at gmail dot com
-Status: Feedback +Status: Open
 [2013-10-30 16:37 UTC] giunta dot gaetano at gmail dot com
You are correct, the provided test file has no problems.
The error I have is with a slightly more complex setup - a composer-generated autoloader class.
I will try to reproduce the error with a minimal setup...
 [2013-10-30 16:42 UTC] gg at ez dot no
Here's a test file with a somewhat odd result:

<?php

use A\B\C as A;

if (!isset($alreadyIncluded)) {
    spl_autoload_register('var_dump');
    $alreadyIncluded = true;
	include __FILE__;
} else {
    class_exists( 'A' ); // 'A'
    new A; // 'A\B\C'
}
 [2013-10-30 16:45 UTC] gg at ez dot no
...and apparently it has nothing to do with re-including the original file (my bad)
 [2013-10-31 16:11 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-10-31 16:11 UTC] nikic@php.net
String class names must always be fully qualified, i.e. if you need to write class_exists('A\B\C'). Namespace aliases don't work in strings.

If you are using PHP 5.5 you can alternatively write class_exists(A::class).
 [2013-10-31 16:14 UTC] gg at ez dot no
OK - but then maybe this could be made more clear in the docs?
 [2013-10-31 16:40 UTC] nikic@php.net
The behavior of namespaces with "dynamic" features in documented in http://php.net/manual/en/language.namespaces.dynamic.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 01 17:01:29 2025 UTC