Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何编写此自动加载器PSR-0/1/2保存?_Php_Syntax_Psr 0 - Fatal编程技术网

Php 如何编写此自动加载器PSR-0/1/2保存?

Php 如何编写此自动加载器PSR-0/1/2保存?,php,syntax,psr-0,Php,Syntax,Psr 0,上的PSR-0/1/2-syntax checker提供此消息 The first parameter of a multi-line function declaration must be on the line after the opening bracket 关于此代码: // class autoloader for PHP 5.3+ spl_autoload_register( function ($class) { include('classes/' . $clas

上的PSR-0/1/2-syntax checker提供此消息

The first parameter of a multi-line function declaration must be on
the line after the opening bracket 
关于此代码:

// class autoloader for PHP 5.3+
spl_autoload_register( function ($class) {
    include('classes/' . $class . '.class.php');
});
我试了很多次,但始终没有得到这段代码PSR-0/1/2-save。 解决方案会是什么样子(或者phphint上的分析仪是否太严格?)

来自:

从PHP5.3.0开始,还可以向回调参数传递闭包

所以,我觉得你所做的一切都很好

你可以看看phphint.org是否同意这一点:

$lambda = function ($class) { include('classes/' . $class . '.class.php'); };
spl_autoload_register( $lambda );

我自己解决了这个问题,但解决方案非常难看

spl_autoload_register( function (
    $class
) {
    include('classes/' . $class . '.class.php');
}
);
也许PSR的人应该想想他们奇怪的语法。 正确的解决方案看起来是这样的,即使是倒退

// autoload function (it's NOT "__autoload", __autoload is
// DEPRECATED since PHP 5.4!)
function autoload($class) {
    include('classes/' . $class . '.class.php');
}

// class autoloader for PHP 5.3+, registers the function that's used if a class/file
// is not found (=autoloader)
spl_autoload_register('autoload');

这里是这个通知的PSR-2语法文档:JanL今天编辑了这个,但是现在它不再是PSR安全的了(根据官方的PSR检查工具)。是的,它现在更可读了,但也坏了。所以我把它重新编辑成了旧版本。使用PSR-1和PSR-2是我的首选。