"Three conventions of the PSR-0 specification":
① namespaces must be consistent with absolute paths
The first letter of the ② class name must be capitalized
③ except for the entry file, the ". PHP" must have only one class (cannot have executable code)
"Developing an infrastructure that complies with the PSR-0 specification":
① all use namespaces
② all PHP files must be loaded automatically and cannot have Include/require
③ Single Entry
Initial directory:
which
index.php Portal File
APP puts all the code related to business logic
Public to place common class files (which appears to cause conflicts, so the folder is renamed Common)
Then add several files under the project folder:
Where common/loader.php implements the automatic loading function of the file, the code is as follows:
<?PHP/*implement file Auto-loading function*/namespace Common;classloader{Static functionAutoLoad$class){ //Var_dump ($class);//string ' Common\object ' (length=13)//$file = BASEDIR. ' /'. Str_replace (' \ \ ', '/', $class). PHP '; Var_dump ($file);//string ' d:\practise\php\design\psr0/common/object.php ' (length=45) requireBASEDIR. ' /‘.Str_replace(‘\\‘, ‘/‘,$class).‘. Php; }}
common/object.php:
<? phpnamespace Common; class Object { staticfunction Test () { echo__method__, ' < Br> '; }} // there must be no executable statement other than the object class
app/controller/home/index.php:
<? phpnamespace app\controller\home; class index{ staticfunction Test () { echo__method__, ' <br> '; }}
When you run the Portal file index.php, app/controller/home/index.php and common/object.php are loaded automatically
<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload ')//Register AutoLoad in the automatic loading of PHP common\Object::test (); App\controller\home\index:: Test ();
Page output:
common\Object::testapp\controller\home\index:: Test
PHP design Pattern Notes and summaries (2) developing the PSR-0 framework