PHP Auto-load Evolution

Source: Internet
Author: User

1. The first time we call the methods in different pages of the class

Test.phprequire_once ' test1.php '; require_once ' test2.php '; Test1\ceshi (); Call way  namespace \ Function name Test2\ceshi ();//Call way  namespace \ Function name
namespace test1;function Ceshi () {    // file full path and filename  such as  D:\sfc\ceshi\mooc\test1.php}
Namespace Test2;function Ceshi () {echo __file__;//file full path and filename  such as D:\sfc\ceshi\mooc\test2.php}

2. Imagine that each file is manually introduced is too cumbersome, if you can use that to introduce the good, some, there is a function __autoload () {} can be implemented

//the solution is to customize the auto-load function and use Spl_autoload_register to register the auto-load functionSpl_autoload_register (' Autoload2 '); Test\test1:: Ceshi ();//call static method format--namespace name \ class Name:: Static method NameTest\test2::Ceshi ();functionAutoload2 ($class){//Define the Introduction file function//echo $class ===> test\test1    List($namespace,$fileName) =Explode(‘\\‘,$class); require__dir__. ' \\‘.$fileName.‘. Php;}
<? phpnamespace Test; class test1{    publicstaticfunction  Ceshi () {        Echo  __method__; Returns the name of the class and the name of the method
Echo ' </br> ';}}

  

1 namespace Test; 2 class test2{3      Public Static function Ceshi () {4         Echo __method__ // Returns the name of the class and the name of the method 5         Echo ' </br> '; 6     }7 }

PHP Auto-load Evolution

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.