Pimple Research and PHP framework construction

Source: Internet
Author: User
Tags php framework

This article refers to PHP container--pimple running Process analysis and the use of Composer step-by-step construction of their own PHP framework, if not clear, please refer to the original text.

Pimple seems to be very popular in the PHP community and is heard to be a very lightweight and usable IOC container. To think about the purpose of applying in the project, to study today.

First to build a simple PHP environment, referring to the above mentioned with composer build, routing using Macaw, created a basecontroller, Indexcontroller, User, Userserviceprovider. Where Basecontroller inherits the container container, which is in the app class in the reference text. Here's a little bit. <? PHP namespace App\controllers;

/** * Created by Phpstorm. * User:hackcat * DATE:2017/7/1 * Time: PM 2:11*/classBasecontrollerextends\pimple\container{
Service provider, all the providers are filled in here, the demo only involves the UserService, of course, there are route what. protected $providers=[Userserviceprovider::class ]; Public function__construct () {Parent::__construct ();
constructor to invoke the registration provider method. $this-registerproviders (); } Public functionAddProvider ($provider){ Array_push($this->providers,$provider); return $this; } Public functionSetproviders ($providers){ $this->providers = []; foreach($providers as $provider){ $this->addprovider ($provider); } } Public functiongetproviders () {return $this-providers; } Public function__get ($id) { return $this->offsetget ($id); } Public function__set ($id,$value) { $this->offsetset ($id,$value); }
The service provider before the loop iteration, registering the provider instance in the container register method, note that this is only the provider instance, not the user. Private functionRegisterproviders () {
foreach($this->providers as $provider){ $this->register (New $provider()); } }}

<? PHP


/* *
* User Service Provider, need to implement
\pimple\serviceproviderinterface interface
* Created by Phpstorm. * User:hackcat * DATE:2017/7/1 * Time: PM 2:27*/ 

classUserserviceproviderImplements\pimple\serviceproviderinterface {
Here to implement the Register method Public functionRegister (\pimple\container$pimple) {
Note that this is the return of an anonymous function instead of returning the instance of user, so it is not equal to $pimple [' user '] = new user (); This will be verified in the test program. Implementation of an on-demand load, deferred loading role. $pimple[' user '] =function($pimple){ return NewUser (); }; }}

<?PHP/**
* The user class, mainly a few test methods and constructors, is used for the next test. * Created by Phpstorm. * User:hackcat * DATE:2017/7/1 * Time: PM 2:26*/classuser{ Public $name; Public function__construct () {Echo' New User '; } Public functionSayHello () {Echo' Hello my name is '.$this-name; }}
<?PHP/**
* Test class. * Created by Phpstorm. * User:hackcat * DATE:2017/7/1 * Time: PM 1:55*/classIndexcontrollerextendsbasecontroller{ Public functionIndex () {
$user=$this-user; $user->name = ' Hackcat '; $user-SayHello (); $user 1=$this-user; Echo $user 1===$user; }}

1. Test 1:

class extends basecontroller{    publicfunction  index () {    }}
No text is displayed on the interface, indicating that the user class is not instantiated.

2. Test 2

 Public function index () {        $user$this, user;         $user->name = ' Hackcat ';         $user-SayHello ();    } Displays the new Userhello my name is Hackcat. Indicates that a user object has been created and the container proves OK. 

3. Test 3

 Public function index () {        $user$this, user;         $user->name = ' Hackcat ';         $user-SayHello ();         $user 1 $this-user;         Echo $user 1, name;    } Again create a User1, not this new User, and the name is also Hackcat, stating $user$user 1, is the same object, singleton mode OK.

Pimple Research and PHP framework construction

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.