我读过几篇关于在php中使用名称空间的文章,但我还没有理解。我有3份文件:
script.php和dog.php位于相同的根文件夹中。类animals.php位于cls文件夹中,狗扩展动物。
资料来源:cls/Animals.php:
namespace cls;
abstract class Animals { .. }Dog.php (没有命名空间,因为它位于根文件夹中):
class Dog extends Animals {...}script.php
function __autoload($cls){
$file = str_replace('\\',DIRECTORY_SEPARATOR,$cls).".php";
if(file_exists($file)){
require_once $file;
} else {
throw new Exception("File not found: ".$file);
}
}
$dog = new Dog("German Shepard");但是我得到了类动物的Not found exception,而$file只是没有命名空间的Animals.php .
__autoload函数的路径中吗?发布于 2013-11-07 12:56:04
如果Dog不在“cls”命名空间中,则必须为动物类编写完整的限定名:
class Dog extends \cls\Animals { ... }https://stackoverflow.com/questions/19836156
复制相似问题