Namespace Autoload works under windows, but not on Linux
Asked Answered
S

5

11

I have the following php code:

index.php

<?php
spl_autoload_extensions(".php");
spl_autoload_register();

use modules\standard as std;

$handler = new std\handler();
$handler->delegate();
?>

modules\standard\handler.php

<?php
namespace modules\standard {
    class handler {
        function delegate(){
            echo 'Hello from delegation!';
        }
    }
}
?>

Under Windows 7, running WAMP, the code produces the message "Hello from Delegation!" however under Linux, I get the following:

Fatal error: spl_autoload(): Class modules\standard\handler could not be loaded in /var/www/index.php on line 15

Windows is running PHP 5.3.0 under WAMP, and Linux is running the 5.3.2 dotdeb package under Ubuntu 9.10.

Is this a configuration issue on my linux box, or just a difference in the way namespaces and autoloading is handled on the different operating systems

Simpleminded answered 19/5, 2010 at 0:40 Comment(1)
This is not the case but, spl_autoload-register() translate the class name to lower case, so it will break on Unix if you use camelCase names ( bugs.php.net/bug.php?id=53065 )Infuriate
P
4

The SPL autoloader is extremely primitive - it has no knowledge of namespaces, so it tries to load a file with \ in it's name while on Linux/Unix the path separator is / not .

Pyrogen answered 20/5, 2010 at 18:59 Comment(1)
Thanks for the heads up. Silly PHP. I'll either use __autoload() or cook something of my own up.Simpleminded
G
3

Herman Radtke says he has submitted a patch :

http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/

:s

I'm hoping it'll be implemented soon.

For now I use this workaround :

<?php
set_include_path( './classes/' . PATH_SEPARATOR . get_include_path() );
spl_autoload_extensions( '.php , .class.php' );
spl_autoload_register();
function linux_namespaces_autoload ( $class_name )
    {
        /* use if you need to lowercase first char *
        $class_name  =  implode( DIRECTORY_SEPARATOR , array_map( 'lcfirst' , explode( '\\' , $class_name ) ) );/* else just use the following : */
        $class_name  =  implode( DIRECTORY_SEPARATOR , explode( '\\' , $class_name ) );
        static $extensions  =  array();
        if ( empty($extensions ) )
            {
                $extensions  =  array_map( 'trim' , explode( ',' , spl_autoload_extensions() ) );
            }
        static $include_paths  =  array();
        if ( empty( $include_paths ) )
            {
                $include_paths  =  explode( PATH_SEPARATOR , get_include_path() );
            }
        foreach ( $include_paths as $path )
            {
                $path .=  ( DIRECTORY_SEPARATOR !== $path[ strlen( $path ) - 1 ] ) ? DIRECTORY_SEPARATOR : '';
                foreach ( $extensions as $extension )
                    {
                        $file  =  $path . $class_name . $extension;
                        if ( file_exists( $file ) && is_readable( $file ) )
                            {
                                require $file;
                                return;
                            }
                    }
            }
        throw new Exception( _( 'class ' . $class_name . ' could not be found.' ) );
    }
spl_autoload_register( 'linux_namespaces_autoload' , TRUE , FALSE );
?>
Granulocyte answered 23/5, 2010 at 23:32 Comment(0)
B
1
function __autoload($class_name) {
$paths[] = dirname(__FILE__) . "/../libs/misc/";
$paths[] = dirname(__FILE__) . "/../../libs/misc/";
$paths[] = dirname(__FILE__) . "/../../libs/helpers/";
$paths[] = dirname(__FILE__) . "/../../libs/simpleimage/";

foreach($paths as $path)
    {
        if(file_exists($path.strtolower($class_name).'.class.php')){
        require_once($path.strtolower($class_name).'.class.php');
        }
    }
}
Bothnia answered 31/1, 2011 at 23:16 Comment(0)
C
1
function __autoload($class_name)
{
    $class_name = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $class_name));

    include $class_name . '.php';
}

The srttolower is needed on Apache because it is (contrary to IIS) case sentive.

Ceremonial answered 30/9, 2014 at 14:45 Comment(0)
M
1

This is a common problem occurs when autoloading. The fix is to use DIRECTORY_SEPARATOR constant in the autoload function.

So your autoload function will look like following

<?php

spl_autoload_register(function($className) {

    $className = str_replace("\", DIRECTORY_SEPARATOR, $className);
    include_once $_SERVER['DOCUMENT_ROOT'] . '/class/' . $className . '.php';

});

If you need to learn more on namespace/class autoloading visit here

Thanks.

Minton answered 20/12, 2017 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.