我从这个例子中得到
http://www.php.net/manual/ru/function.spl-autoload.php#92767
但这会导致错误
* fatal error :spl_autoload() [function.spl-autoload]:第 18 行无法在 C:\my_projects\site.local\www\index.php 中加载类空间*
/index.php
// Your custom class dir
define('CLASS_DIR', 'oop/classes');
// Add your class dir to include path
set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);
// You can use this trick to make autoloader look for commonly used "My.class.php" type filenames
spl_autoload_extensions('.class.php');
// Use default autoload implementation
spl_autoload_register();
new space/my;
/oop/classes/space/my.class.php
namespace space;
class my{
public function __construct{
var_dump('space class was here!');
}
}
我知道 PSR-0,但就我而言,我需要了解内置函数的工作原理
最佳答案
new space/my;
应该
new space\my;
注意:您错过了
()
的__construct
在 my.class.php
关于php spl_autoload 不适用于语法错误的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12107161/