shang't
$path = "a\b\\{$className}".".php";
主要是上面这条路径,使用use也不能访问被namespace封装后的代码,
未进行封装的代码倒是可以
下面这条倒是所有都可以访问
想知道原因
$path = $className.".php";
附上全部代码
//test1的
<?php
class test1
{
public function echo()
{
echo "this is class Test in a\b\c";
}
}
?>
//test2的
<?php
namespace a\b;
class test2
{
public function echo()
{
echo "this is class Test2 in a\b";
}
}
?>
//第一种文件路径的
<?php
function loader1($className)
{
echo "$className<hr/>";
$path = "a\b\\{$className}".".php";
if(file_exists($path)){
require_once($path);
}else{
echo"file:{$path} is not exsists<hr/>";
}
}
spl_autoload_register('loader1');
$t = new test1();
$t -> echo();
?>
//第二种文件路径的
<?php
spl_autoload_register(function($className){
echo "$className <hr/>";
$path = $className.".php";
if(file_exists($path)){
require_once($path);
} else {
echo "file: {$path} is not exsists<hr/>";
}
});
use a\b\test2;
$t2 = new test2();
$t2-> echo();
echo "<hr/>";
$t3 = new test1();
$t3 -> echo();
?>
a,b文件夹已经在站点根目录下建立。