Understanding Dependency Injection (Dependency injection)

Source: Internet
Author: User
Tags sessionstorage

Understanding Dependency Injection

Yii2.0 uses the idea of dependency injection. It is using this mode that makes YII2 exceptionally flexible and powerful. Do not think this is a very iffy thing, read the following two examples to understand.

classsessionstorage{function__construct ($cookieName= ' php_sess_id ')  {    Session_name($cookieName); Session_Start(); }  functionSet$key,$value)  {    $_session[$key] =$value; }  functionGet$key)  {    return $_session[$key]; }  // ...}

This is an operation session class, the following user class to use the function in the session

classuser{protected $storage; function__construct () {$this->storage =NewSessionstorage (); }  functionSetLanguage ($language)  {    $this->storage->set (' Language ',$language); }  functionGetLanguage () {return $this->storage->get (' language '); }  // ...}

We can call it that way.

$user New User (); $user->setlanguage (' zh ');

This doesn't seem to be a problem, but the user has written the Sessionstorage class, the coupling is too strong, not flexible enough to be decoupled.

Optimized user Class Code

classuser{protected $storage; function__construct ($storage)  {    $this->storage =$storage; }  functionSetLanguage ($language)  {    $this->storage->set (' Language ',$language); }  functionGetLanguage () {return $this->storage->get (' language '); }  // ...}

Call Method:

$storage New Sessionstorage (' session_id '); $user New User ($storage);

Instead of creating a Sessionstorage object in the user class, the storage object is passed as a parameter to the user's constructor, which is dependency injection .

The way of dependency injection

Dependency injection can be done in three different ways:

Constructor Injection Constructor Injection

This is also the most common one that has just been used:

class user{  function __construct ($storage)  {    $this$ Storage;  }   //  ...}
Set Value injection Setter injection
class user{  function setsessionstorage ($storage)  {    $this  $storage;  }   //  ...}
Attribute Injected Property Injection
class user{  public$sessionStorage;} $user $storage;

Here's an example of Yii2.

// Create a Pagination object with the total count $pagination New $count ]); // limit the query using the pagination and retrieve the articles $articles $query->offset ($pagination,offset)    ->limit ($pagination ,limit)    ->all ();

Reference: http://fabien.potencier.org/what-is-dependency-injection.html

Understanding Dependency Injection (Dependency injection)

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.