Autoload namespace works under windows, but not on Linux

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!'; } } } ?> 

On Windows 7 running WAMP, the code displays the message "Hello from delegation!". however under linux i get the following:

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

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

Is this a configuration problem in my Linux module or is it just different in the way namespaces and startup are handled on different operating systems.

+11
php namespaces autoload
May 19 '10 at
source share
5 answers

The SPL autoloader is extremely primitive - it does not know the namespace, so it tries to load a file with the name \ in it, and on Linux / Unix the path separator / not.

+4
May 20 '10 at 18:59
source share

Herman Radtke says he sent the patch:

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

: S

I hope this will be implemented soon.

Now I am using 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 ); ?> 
+2
May 23 '10 at 23:32
source share
 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'); } } } 
+1
Jan 31 '11 at 23:16
source share
 function __autoload($class_name) { $class_name = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $class_name)); include $class_name . '.php'; } 

Apache requires srttolower because this (unlike IIS) is random.

+1
Sep 30 '14 at 14:45
source share

This is a common problem that occurs during startup. The fix is ​​to use the DIRECTORY_SEPARATOR constant in the startup function.

So your startup function will look like this

 <?php spl_autoload_register(function($className) { $className = str_replace("\", DIRECTORY_SEPARATOR, $className); include_once $_SERVER['DOCUMENT_ROOT'] . '/class/' . $className . '.php'; }); 

If you need to know more about autoload namespace / class, visit here

Thank.

0
Dec 20 '17 at 6:54
source share



All Articles