|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
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); });