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:
- __autoload use tips.
- Namespaces are described and used.
- 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.