Reference article: http://www.yuansir-web.com/2014/03/20/%E7%90%86%E8%A7%A3php-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% A5laravel-ioc%e5%ae%b9%e5%99%a8/?preview
When we are building a class, it is possible to use another object instance inside the class to raise a chestnut:
class user{ publicfunction getUser () { $dbnew db ();// This generates a dependency on the database connection class $db->select (' Select username form User: ' ); ... }}
When we modify the DB this class, we must come back to modify the user class, you can imagine the DB class is definitely not only used in the user class, a project to have many classes to use the DB class, If the DB class has been modified (for example: The DB class has been renamed), you must modify all classes in the project that use the DB class.
It can be solved this time.
class user{ publicfunction$db) { //$db = new db ();// Dependency disappears $db->select (' Select username form User: ' ); ... }}
To invoke the GetUser method at this time, you have to "inject" $db.
In fact, our user class is certainly not only used to getuser this method, there may be used GetUserName, Getuserage, Getusersex ... Wait, are we going to add a $db parameter to each method? Of course it's not possible, so let's refine this user class:
classuser{Private $db; //* Public function__construct (DB$db){ $this->db =$db; } /*/Public Function Getdb (DB $db) {$this->db = $db; } //*/ Public functionGetUser () {$this->db->select (' Select username form User: '); ... } Public functionGetuserage () {$this->db->select (' Select userage form User: '); ... } Public functionGetusersex () {$this->db->select (' Select usersex form User: '); ... }}
You can see that we can inject $db into the constructor and the Getdb method.
Well, we've achieved "dependency reversal"!!! What if we need other classes for the user class? To give a very inappropriate, far-fetched chestnut:
We put the username in the memcache, put the userage in the session, and put the Usersex in Redis (this ...). Very unrealistic) That's what our class looks like.
classuser{Private $db; Private $mem; Private $redis;
...
Public function__construct (DB$dbMem$mem, Redis$redis, Session $session ...){ $this->db =$db; $this->mem =$mem; $this->redis =$redis; } Public functionGetUser () {$this->db->select (' Select username form User: '); ... } Public functionGetuserage () {$this->mem->get (' Userage '); ... } Public functionGetusersex () {$this->redis->get (' Usersex '); ... }}
To put so many instances in the controller. And if you want to use the user class, there are instances of so many storage classes, not tired? The code must be a mess!
This time you can use a factory to "perfect" a bit:
class userfactory{ publicstaticfunction getuserobj () { $db New DB (); $mem New Mem (); ... $user New ($db$mem , ... ); return $user ; }}
This is much more beautiful, but!!! And then we're going to instantiate each storage class (Db,mem,redis ... )
Now the service container (IoC) comes in:
classuser{Private $IoC; Public function__construct (IoC$IoC){ $this->IOC =$IoC; } Public functionGetUser () {$this->ioc->get (' db ')->select (' Select username form User: ')); ... } Public functionGetuserage () {$this->ioc->get (' mem ')->get (' Userage ')); ... } Public functionGetusersex () {$this->ioc->get (' Redis ')->get (' Usersex ')); ... }}$IoC=NewIoC ();//The closure function is used here, and the->set is not actually instantiated when it is $di,//But the anonymous function is executed at $di->get () and the object is returned. $IoC->set (' db ',function(){ return NewDB (.....);});$IoC->set (' Mem ',Funciton () {return NewMem (...);});$IoC->set (' Redis ',Funciton () {return NewRedis (.....);});$user=NewUser ($IoC);
Done!!!!!!
Di and IOC are so simple.
Share several links:
Service container in Symfony:
Http://www.cnblogs.com/Seekr/archive/2012/06/19/2554934.html
listen to Fabien potencier talk about Symfony2 's "What is Dependency injection?" "
Http://www.cnblogs.com/Seekr/archive/2012/06/20/2556463.html
There are also Laravel, struts2, and spring articles about dependency injection that can be searched by themselves.
Dependency Injection (DI) and service container (IoC)