最近一直用Thinkphp框架开发,3.2版本的,用到了命名空间.遇到了一个坑:
A.class.php
<?php
namespace nsa;
class A {
function fa(){
echo 'fa';
}
}
b.php
<?php
require_once('A.class.php');
use nsa\A;
$a = new A(); //这样不会错
$a->fa();
$type = 'A'; //这样写,new时会出错
$aa = new $type(); //错误信息 Class 'A' not found
$aa->fa();
$type2 = 'nsa\\A'; //写成这样就对了,可以new了
$aaa = new $type2();
$aaa->fa();
大概意思就这样,记下这个坑.