PHP design Pattern Notes and summaries (2) developing the PSR-0 framework

Source: Internet
Author: User
Tags autoload

"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

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.