我有三個檔案firstFile.php,secondFile.php和thirdFile.php.
firstFile 如下所示:
namespace test\main;
function hello () {
echo 'from main';
}
secondFile 如下所示:
namespace test\second;
function hello () {
echo 'from second';
}
thirdFile 如下所示:
use function test\main\hello;
echo hello();
我的問題是為什么use function test\main\hello;有效,但是當我使用時use function test\second\hello;,它會導致致命錯誤:Uncaught Error: Call to undefined function test\second\hello()?
uj5u.com熱心網友回復:
如果您在一個檔案中使用所有功能,則會出錯。因為檔案的所有內容都包含hello函式。但是,您需要使用 autoload.php 或自定義自動加載模式。否則,您可以在使用之前使用 require 或包含所有這些檔案。
例如
require "firstFile.php"
require "secondFile.php"
use function test\main\hello;
use function test\second\hello as secondHello;
echo hello();
echo secondHello();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/524366.html
標籤:php命名空间
