Php 需要一次罐';t在Windows 10上加载现有文件(适用于Linux)
我们的自动加载器有一个神秘的问题:Php 需要一次罐';t在Windows 10上加载现有文件(适用于Linux),php,windows,psr-4,Php,Windows,Psr 4,我们的自动加载器有一个神秘的问题: function psr4_default_autoload( $class ) { // project-specific namespace prefix $prefix = 'basefolder\\'; // base directory for the namespace prefix $base_dir = SOURCE_DIR . '/'; // does the class use the names
function psr4_default_autoload( $class )
{
// project-specific namespace prefix
$prefix = 'basefolder\\';
// base directory for the namespace prefix
$base_dir = SOURCE_DIR . '/';
// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr( $class, $len );
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
if ( file_exists( $file ) ) {
require_once $file;
}
}
SOURCE\u DIR
是一条绝对路径。否则,这就是原始的psr4自动加载器示例:
这个自动加载器在我的Linux PC和我们的服务器上工作。但是,在Windows上,file\u存在($file)
返回true,但require\u once$file代码>不起作用。如果我们回显$file
,它将返回我们想要加载的文件,并且该文件也确实存在于那里
错误消息是:
致命错误:在第14行的C:\xampp\htdocs\xyz\classes\DatabaseAbstraction\Entity\UserLogin.php中找不到类'basedir\DatabaseAbstraction\AEntity'
这一行是:类UserLogin扩展了AEntity
。背景:
namespace basedir\DatabaseAbstraction\Entity;
use basedir\DatabaseAbstraction\AEntity;
class UserLogin extends AEntity
有什么想法吗?Windows的目录分隔符是“\”而Linux的是“/”;您应该使用“DIRECTORY\u SEPARATOR”关键字来表示用于创建路径的字符。它在运行时确定,具体取决于当前操作系统
在您的情况下,这将给出:
$base_dir = SOURCE_DIR . DIRECTORY_SEPARATOR;
[...]
$file = $base_dir . str_replace( '\\', DIRECTORY_SEPARATOR, $relative_class ) . '.php'; // assuming your $base_dir follows the same logic
您还必须相应地修改源目录。仅用于记录:
文件AEntity.php以开始,谢谢,但这并没有解决它。我编辑了我的问题。自动加载程序适用于某个类,但不适用于它扩展的类。@Jaime:哪个Windows版本?哪个PHP版本?我会对此感兴趣:)现在我会保留我的答案,因为我相信无论如何你都会面临这个问题。。。关于编辑,触发错误时,$file
的内容是什么?使用$file
一切正常。中缺少了一个php,至少您现在有了工作。不过,最好为此添加检查;)短标记是php.ini中的一个配置选项。这两台计算机的设置可能不同。我建议始终使用