PHP framework application-level objects

Source: Internet
Author: User
When we see the Yii framework, there is also the Discuz source program. We like to create an application object before initialization and dispatch. What is the purpose of this object? Or is there any benefit? Generally, after initialization and configuration, routes are distributed to ControllerActio... when we see the Yii framework, there is also the Discuz source program. We like to create an application object before initialization and dispatch. What is the purpose of this object? Or is there any benefit?

Generally, after initialization and configuration, routes are distributed to the Controller/Action level for execution. Why is there a layer of application object in the middle?

The PHP Execution end after the lifecycle is completed. Is require/include/autoload equivalent to a file at the end? I can define/$ _ SERVER/$ _ ENV for the required global parameters and public static function for the required global methods. What is the purpose of the application object mentioned above?

Reply content:

When we see the Yii framework, there is also the Discuz source program. We like to create an application object before initialization and dispatch. What is the purpose of this object? Or is there any benefit?

Generally, after initialization and configuration, routes are distributed to the Controller/Action level for execution. Why is there a layer of application object in the middle?

The PHP Execution end after the lifecycle is completed. Is require/include/autoload equivalent to a file at the end? I can define/$ _ SERVER/$ _ ENV for the required global parameters and public static function for the required global methods. What is the purpose of the application object mentioned above?

This APP object is useful:

  1. Test: an application is an app object. Therefore, during unit test, you only need to create an app object-test-destroy an app object. In this way, you can easily set setup and teardown before and after the test; in addition, the app objects can be inherited, and polymorphism can be used to pile up and unit tests can be easily implemented.
  2. Avoid global variables: global variables are evil ~ Eliminate global variables ~ But what if many configurations are global? Put them into this app object.
  3. Different types of app objects can be used to easily change the program's external interface. In the Yii framework, a CWebApplication is a normal web application, and a CConsoleApplication is changed to a console application, it's amazing to say ~
  4. I haven't figured it out...

Similar app objects are generally used as service containers. For example, symfony2 and silex of the mini model share the same concept. Some instantiated objects, configuration items, and other items are inserted into this app object, so long as you can access the app object, you can get these things. I like this method very much.

There is an abstract lightweight library called pimple that specifically serves as a dependency injection container. You can understand it if you have time.

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.