在名字空间中如何使用php 的内置类?

zhiys 2012-10-02 12:15:39
你好,初学php,遇到问题,求教:
一下是一个php 文件中的代码。包含了2个名字空间,test2 和test4 , 在test2中 Exception 类抛出错误
在test4 空间中检测错误,但是运行时说 test2 中找不到 Exception类,所以在红色部分的 Exception类
前面添加 \ 指明根空间,但是在 test4 中又提示 catch 语句中 Exception $e 错误
请问我该如何修改才正确,谢谢。
namespace test2{
class A{
private $file_id ;

public function file_open($path_file_name){
$this->file_id=@fopen($path_file_name , 'r');
if($this->file_id==false){ throw new \Exception("path_file_name isnot EXIST");
echo(nl2br(sprintf("file open failed\n"))); }
}
}
}

namespace test4{
use test2;
try{
$obj_A = new test2\A();
$path_name = "/home/zhiys/php/111.php";
$obj_A->file_open($path_name) ;
}catch(Exception $e){ ... }

}
...全文
50 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
acabin 2012-10-02
  • 打赏
  • 举报
回复
catch(\Exception $e)

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧