Domanda

I have a problem here. I have created the namespace for all classes. previously i was using windows 7 to develop current app, everything is fine. now i just moved to ubuntu, the problem comes.

index.php

spl_autoload_extensions(".php");
/*spl_autoload_register(function ($class) {
    require __DIR__ . '/../' . $class . '.php';
});*/
//provided i have tried the above method, which works on windows 7 but not Ubuntu
spl_autoload_register(function ($class) {
    require '/../' . $class . '.php';
});

//for your info, i do this
//require "../resources/library/Config.php";
//it works, no error

use resources\library as LIB;
use resources\dal as DAL;

//instantiation
$config = new LIB\Config();
print_r($config->fbKey());

i got this error

PHP Warning:  require(../resources\\library\\Config.php): failed to open stream: No such file or directory in /home/user/dir1/dir2/index.php

i cannot find the error. hope you guys can help me with this. any question don hesitate to comment i will edit. thanks in advance.

UPDATE - Extra Info

PHP version 5.4.6

LATEST UPDATE

any idea how to solve this without using str_replace ? JULP and MOONWAVE have the answer!

È stato utile?

Soluzione 2

Which PHP version do you use? (in PHP < 5.3.3, default spl_autoload_register was not namespace aware - I mean \ was not replaced by / on Unix systems)

In your code, try to replace:

require __DIR__ . '/../' . $class . '.php';

By:

require __DIR__ . '/../' . str_replace('\\', '/', $class) . '.php';

=> Always use slashes in path, they work everywhere. Backslashes, don't. And don't forget filenames are case sensitive on Unix systems, the opposite of Windows.

Altri suggerimenti

You have a problem with your directory separator, as it is different in Win and Unix.

To be consistent, you should update your autoload function:

spl_autoload_register(function ($class) {
    require '/../' . str_replace("\\", DIRECTORY_SEPARATOR, $class) . '.php';
});

EDIT: the solution is pretty wide accepted in production environments.

You should familiarize yourself with the use of PHP's DIRECTORY_SEPARATOR constant if you are going to be doing cross-platform development.

The error message there should tell you all you need to know. You are not generating proper LINUX file paths.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top