PHP namespace Learning notes and testing

Source: Internet
Author: User
Tags php and php code


The PHP namespace is PHP5.3 start support. This article explains the PHP namespace usage and the PHP namespace detailed. It's the birth that makes us in a file you can use multiple classes with the same name without conflict.
Benefits: Our project has a class of log files, called log. Then we have to introduce another code package, which also has a class called log. Then in a file, we log the logs and need to write a log for two classes. Can class have the same name, how to do? At this time, the namespace came into being. Namespaces are already supported in languages such as Java, and I'm big PHP until 5.3 to provide support for namespaces.
Example one:
File index.php
<?php
Include ' test.php ';

Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}
$obj = new index ();
$obj->a ();
Echo ' <br> ';
$obj 1 = new Test\index ();
$obj 1->a ();
?>

File test.php
<?php
namespace test;
Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}
?>

We do not set the namespace for index.php and set the namespace for test.php, named Test. Run index.php.
Results:
index.php
Index:index::a
test.php
Test\index:test\index::a

We see that a class with the same name can run without conflict.

Example two:
File index.php
<?php
namespace index;
Include ' test.php ';

Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}
$obj = new index ();
$obj->a ();
Echo ' <br> ';
$obj 1 = new\test\index ();
$obj 1->a ();
?>

File test.php
<?php
namespace test;
Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}
?>

We set the namespace for index.php, named Index, and set the namespace for test.php, named Test. Run index.php.
Results:
index.php
Index\index:index\index::a
test.php
Test\index:test\index::a

Compare example one and two, do not set the namespace for index.php, that is, the file is a file under the entire PHP global namespace, then use Test\index (), if you set the namespace for index.php, that is, in other namespaces to use the namespace, there will be one more "\", you should use \test\index ().

Example three:
File index.php
<?php
namespace index;

Include ' namespace.php ';

Use\test\test1\test2 as Test2;

Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}

$obj = new index ();
$obj->a ();

Echo ' <br> ';

$obj 1 = new\test\test1\test2\index ();
$obj 1->a ();

Echo ' <br> ';

$obj 1 = new Test2\index ();
$obj 1->a ();

File test.php
<?php
namespace Test\test1\test2;
Class index{
Public function A () {
Echo basename (__file__);
Echo ' <br> ';
Echo __class__. ' : ' . __method__;
}
}

Results:
index.php
Index\index:index\index::a
test.php
Test\test1\test2\index:test\test1\test2\index::a
test.php
Test\test1\test2\index:test\test1\test2\index::a

What does that mean? Alias! You've used SQL.
Select COUNT (*) as ' count ' from ' teblename '

Well, a meaning. \test\test1\test2 This name is too long, just alias for Test2 good. After using use, the namespace is thought to be under the index namespace instead of a member of the global namespace, so use Test2\index () instead of \test2\index ().

The alias is executed when the PHP code is compiled, and the variables are parsed later. This means that the USE keyword cannot be applied to variables. Examples are as follows (excerpted from the official manual example):
<?php
Use My\full\classname as Another, My\full\nsname;

$obj = new Another; Instantiate a My\full\classname object
$a = ' Another ';
$obj = new $a; Materialized a Another object

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.