Usage of php namespaces

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.