php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51991 spl_autoload and *nix support with namespace
Submitted: 2010-06-03 19:23 UTC Modified: 2010-07-28 00:20 UTC
From: grummfy at gmail dot com Assigned: felipe (profile)
Status: Closed Package: SPL related
PHP Version: 5.3.2 OS: linux, mac, *nix
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: grummfy at gmail dot com
New email:
PHP Version: OS:

 

 [2010-06-03 19:23 UTC] grummfy at gmail dot com
Description:
------------
On windows systems you can use spl_autload to load namespaced class and it works!
On *nix system you can't!

Tested on linux (ubuntu 10.04 64 bit) with package : php5 5.3.2-1ubuntu4.2

You can test the next script on a windows and on a linux or a mac.


Test script:
---------------
create a file called index.php
------------------------------
use My\Framework\Test;
spl_autoload_register();
$test = new Test();
echo $test;
------------------------------
Another file in a subdir My/Framework called Test.php
------------------------------
namespace My\Framework;
class Test
{
    public function __toString()
    {
        return 'hi';
    }
}
------------------------------

Expected result:
----------------
Expected result :
script that produce :
hi

Actual result:
--------------
windows:
hi
Linux:
Fatal error: spl_autoload() [<a href='function.spl-autoload'>function.spl-autoload</a>]: Class My\Framework\Test could not be loaded in /.../index.php on line 7

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-04 02:10 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=300176
Log: - Fixed bug #51991 (spl_autoload and *nix support with namespace)
 [2010-06-04 02:10 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2010-06-04 02:10 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-06-04 02:44 UTC] grummfy at gmail dot com
thanks
 [2010-07-26 23:01 UTC] xstansa at gmail dot com
I just upgraded to PHP 5.3.3 and this bug still persists. 

Tested on Linux (Cent OS 5.5) with PHP 5.3.3.

Same Test Script and result as the original bug.
 [2010-07-26 23:54 UTC] felipe@php.net
Hi, make sure you are using the right version... (phpversion())
 [2010-07-27 02:58 UTC] xstansa at gmail dot com
I've already checked that using phpinfo(). It is PHP 5.3.3.
 [2010-07-27 03:34 UTC] felipe@php.net
Hi, I forgot to say... the path that will be tried to be found it is lowercased. 
i.e. my/framework/test.php

See bug #52406

Thanks.
 [2010-07-27 20:05 UTC] xstansa at gmail dot com
Are you saying that I'll have to rename all my php file names to be lowercase on 
Linux to use spl_autoload?
 [2010-07-28 00:20 UTC] felipe@php.net
Exactly. You can implement your custom autoload to behaves as you want. The lowercased stuff is good for some people, but is not the desired for others.
 [2011-02-23 03:36 UTC] mike at digitalstruct dot com
This is hard to believe that automatically lowercasing the path is not a bug... 
the majority of PHP users are actually on *nix platforms - especially in 
production.  Causing the paths to automatically be lowercase drastically takes 
away from the default functionality.

I apologize if I am being arrogant here - however, when you write include 
'My/Path/To/A/File.php' it does not lowercase that path so why should 
spl_autoload do anything different with the default behavior?

Offending code to fix this is on line: 303 in trunk of php_spl.c: 303 >---
lc_name = zend_str_tolower_dup(class_name, class_name_len);

Removing the str_tolower would resolve this issue and we would all be happy.  I 
will post to php-dev as well with a patch.
 [2013-11-09 22:54 UTC] moon at quantentunnel dot de
After searching around a while I found a workaround without performance loss.

1. Slower but not working with camelcase class files (like "MyClass.php"):
spl_autoload_register(function($classname) { require_once(__DIR__ . '/' . str_replace('\\', '/', $classname) . '.php'); });

2. Faster but not working with camelcase class files:
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);
spl_autoload_extensions(".php");
spl_autoload_register();
spl_autoload_register( function($classname) {} );

3. Faster and working with camelcase class files:
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);
spl_autoload_extensions(".php");
spl_autoload_register(function ($classname) { spl_autoload($classname); });
 [2013-11-09 23:36 UTC] moon at quantentunnel dot de
I'm sorry, my last post was not correct, case 3 does not work :-(
 [2014-01-15 12:10 UTC] broken at propeople dot com dot ua
Bug is reproduced on 5.4.4.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jan 31 03:01:29 2025 UTC