您的位置:首頁技術文章
文章詳情頁

命名空間的問題 請哪位老鳥來幫忙分析下 ,我是小白

瀏覽:102日期:2022-06-04 16:29:48

問題描述

//namespace1.php<?phpnamespace Demo1;class test1{private $name='www.php.cn';public function getName(){return $this->name;}}

//namespace2.php<?phpnamespace Demo2;require('namespace1.php');use Demo1test1;class test1{private $name='PHP中文網';public function getName(){return $this->name;}}

問題就是 我在訪問namespace2.php時會為什么出現如下錯誤

Fatal error: Cannot declare class Demo2test1 because the name is already in use in

難道必須要 use Demo1test1 時還要給類取個別名嗎?? ‘as D1test1'????

請指導一下

問題解答

回答1:

demo1 中 已存在 test1類了。然后你又應用了文件require('namespace1.php');和 然后又引用了demo1的類 use Demo1test1;

你感覺下會不報錯嗎? 你給他as 個別名試試?

国产综合久久一区二区三区