使用 __NAMESPACE__ 取得當前文件的 Namespace
呼叫 Namespace 裡的 function
/**
* https://vector.cool
*/
Namespace VECTOR\COOL;
function test() {
echo "Hello world!\n";
}
call_user_func(__NAMESPACE__ .'\test');
呼叫 Namespace 裡的 class 中的 function
/**
* https://vector.cool
*/
Namespace VECTOR\COOL;
class Hello {
static public function test() {
echo "Hello world!\n";
}
}
call_user_func(__NAMESPACE__ .'\Hello::test'); // String
call_user_func(array(__NAMESPACE__ .'\Hello', 'test')); // Array
在 WordPress Action 中呼叫
/**
* https://vector.cool
*/
Namespace V123\PLUGIN\CFU;
add_action('init',__NAMESPACE__.'\create_initial_admin_menu');
參考資料:
http://php.net/manual/en/language.namespaces.nsconstants.php
https://stackoverflow.com/questions/14682356/relative-namespaces-and-call-user-func[……]
閱讀更多