Php namespace usage problems this post was last edited by potency at 2014-03-2009:46:47 test1.php: & lt ;? Phpnamespace & nbsp; test1; function & nbsp; test () {& nbsp; echo & nbsp ;__ use of FUNC php namespace
This post was last edited by potency at 09:46:47
Test1.php:
Namespace test1;
Function test ()
{
Echo _ FUNCTION __;
}
[/Code]
Test2.php
use test1;
function test2()
{
echo __FUNCTION__;
}
echo test1();
The two php files are all under the same subdirectory test.
The following error occurs when you run http: // localhost/test/test2.php in a browser:
----------
(! ) Warning: The use statement with non-compound name 'test1' has no effect in E: \ wamp \ Apache \ htdocs \ robot \ test \ test2.php on line 6
(! ) Fatal error: Call to undefined function test1 () in E: \ wamp \ Apache \ htdocs \ robot \ test \ test2.php on line 11
Call Stack
# TimeMemoryFunctionLocation
10.0020134912 {main} () .. \ test2.php: 0
--------------
Very strange. Does anyone know what the problem is? My php is 5.4
------ Solution --------------------
include 'test1.php';
//use test1;
function test2()
{
echo __FUNCTION__;
}
echo test1\test();
------ Solution --------------------
namespace test1;
include 'test1.php';
function test2()
{
echo __FUNCTION__;
}
echo test();
------ Solution --------------------
Functions and constants do not support use import rules. only classes support import rules.
And use test1; this method is not correct.
Just like use test1/classname. When instantiating the function below
New classname; // php will find the classname class in the test1 namespace. if it is not found, it will be automatically loaded.
------ Solution --------------------
For example, when using use, the moderator should look like the following:
Use test1 \ test as test2;
Or
Use test1 \ test; this is actually equivalent to use test1 \ test as test;
The test1 above is the namespace followed by the test class under the test1 name
Then instantiate new test (), which is equivalent to new test1 \ test ();
This is the role of use, that is, alias.