PHP __autoload functions and namespaces.

Source: Internet
Author: User

After 5.3, there is a namespace, where the API found in other people's homes rarely contains require,include code, but it implements the inclusion of all files.

Article directory:
    1. __autoload use tips.
    2. Namespaces are described and used.
    3. Both use optimized code together.

1,__autoload Tips for use:

Principle: When a __autoload function is defined, the function is triggered every time an object of a class is created.

Prototype:

function __autoload ($classname) {    $classname=str_replace(' \ \ ', ' /',$classname);     require ‘.. /src/'. $classname. " php ';}

Automatic loading with __autoload:

function__autoload ($classname) {Requireall (‘.. /src ',$class _name)}/** * Search all the files in the $dir directory, if there is a file named $class _name, include it. */functionRequireall ($dir,$class _name){    $d=dir($dir);  while(false!==($entry=$d-read ())) {        if($entry= = '.. ')Continue; if($entry= = '. ')Continue; if(Is_dir($dir.‘ /‘.$entry) {Requireall ($dir.‘ /‘.$entry,$class _name); }Else{            if($entry==$class _name.‘. Php){                require_once $dir.‘ /‘.$entry; }        }    }    $d-close ();}

In the directory, regardless of the level, as long as you define the class, and the name and file name are the same, you can create the object of this class.

2,namespaces Introduction and use

Namespaces are designed so that class name definitions do not repeat, code that does not use namespaces tends to have many more bytes in order to differentiate between functions of a similar function, and sometimes conflicts with system-defined functions, and namespaces are designed to avoid this kind of thing.

Namespace rules:

The default is the public namespace, which intercepts this paragraph on the Internet:

$a=new foo (); or Foo::staticmethod ();. If the current namespace is Currentnamespace,foo, it will be resolved to Currentnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Foo. Warning: If a function or constant in a namespace is undefined, the unqualified function name or constant name is resolved to the global function name or constant name. For details, see Using namespaces: Fallback global function name/ constant name.
Qualifying name $anew subnamespace\foo (); or Subnamespace\foo::Staticmethod ();. If the current namespace is Currentnamespace, Foo will be parsed to Currentnamespace\subnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Subnamespace\foo.
$a new \currentnamespace\foo (); or \currentnamespace\foo::staticmethod ();. In this case, Foo is always parsed into the literal name in the code (literal name) Currentnamespace\foo

Define multiple namespaces:

namespace MyProject {ConstCONNECT_OK = 1;classConnection {/* ... */ }functionConnect () {/* ... */}}namespace Anotherproject {ConstCONNECT_OK = 1;classConnection {/* ... */ }functionConnect () {/* ... */  }}
3, both use optimized code together

In fact, the principle is very simple, when "new Foo ()" Time, will be parsed into Currentnamespace\foo, so in __autoload make a change can be implemented automatically loaded.

  

 Usewish\wishclient;function__autoload ($classname){    $classname=Str_replace(‘\\‘,‘/‘,$classname); require‘.. /src/'.$classname.‘. Php;}/** * Search all the files in the $dir directory, if there is a file named $class _name, include it. */functionRequireall ($dir,$class _name){    $d=dir($dir);  while(false!==($entry=$d-read ())) {        if($entry= = '.. ')Continue; if($entry= = '. ')Continue; if(Is_dir($dir.‘ /‘.$entry) {Requireall ($dir.‘ /‘.$entry,$class _name); }Else{            if($entry==$class _name.‘. Php){                require_once $dir.‘ /‘.$entry; }        }    }    $d-close ();}$A=NewWishclient (");

  

PHP __autoload functions and namespaces.

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.