PHP Design Patterns-Namespaces and auto-onboarding

Source: Internet
Author: User

About namespaces:

  The earliest PHP is the concept of no namespace, so that can not have the same name of the class or function, when the project becomes larger, the likelihood of conflict is higher, the code will become larger, in order to plan, from php5.3 began to support the namespace.

Description Code:

test1.php
<?php//Declaration namespace namespace Test1;function test () {echo "test1<br/>";}
test2.php<?php//Declaration namespace namespace Test2;function test () {echo "test2<br/>";}

Introduce test1.php, test2.php into the test.php:

test.php<?php//introduction of Test1,test2require ' test1.php '; require ' test2.php ';//namespace use Test1\test (); Test2\test ();

If you do not use namespaces, it is obvious that PHP will repeat the fatal error for the function name if the command space results are as follows:

Test1test2

About Automatic loading: 

Before PHP is introduced through include or require to PHP, when the project is getting bigger and larger, if a PHP file needs to introduce dozens of PHP classes, it will introduce dozens of lines, which is very inconvenient to manage code and development. The automatic loading of classes is provided after php5.2.

The __autoload method is introduced in php5.2, but when multiple PHP files use this method at the same time there is a possibility that the function name will be duplicated, and in php5.3 the function is discarded, and the system provides a method of Spl_auto_register (). Conflicts can be avoided when the class is automatically loaded into Spl_auto_register.

test3.php
Class Test3{static function Test () {echo "test3-class<br/>";}}
test4.php
<?phpclass test4{static function Test () {echo "test4-class<br/>";}}
<?phpspl_autoload_register (' autoload1 ');//function name passed in as parameter to Spl_autoload_register (' autoload2 ');//function name passed in as parameter can support multiple TEST3 :: Test (); Test4::test (); function Autoload1 ($class) {require __dir__. ' /'. $class. '. PHP '; }function autoload2 ($class) {require __dir__. ' /'. $class. '. php ';}

  

The results are as follows:

Test3-classtest4-class

* When PHP executes the class that you use doesn't exist, PHP tells the class name (TEST3) to automatically load the function (AutoLoad), and then we just need to introduce the relevant class.

Summary: Namespaces and Auto-onboarding are important for us to write about object-oriented development.

PHP Design Patterns-Namespaces and auto-onboarding

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.