A TEXT POST

PHP Dependency Injection with Service Locator

In PHP, Dependency Injection lets us inject instances of objects into other objects so we don’t have to rely on using Singletons and even worse, Globals.  In this tutorial, we are bending the rules of dependency injection slightly by assigning dependencies to static keywords rather than setting via a method.

To explain, first of all let me show you how you would normally access an instance of a class from within another class method using a Global.

class globalClass{
  private $myGlobalClassProperty = '3';
  
  public function getMethod() {
    return $this->myGlobalClassProperty;
  }
}

$myGlobalClass = new globalClass();

class myClass() {

  private function myClassMethod() {
    global $myGlobalClass;
    return $myGlobalClass->getMethod();
  }
}

The above usage of globals isn’t ideal, so how do we access an object instance from within another object?  This is where dependency injection comes into play.  First we need to create a controller, you can also call this a container if you prefer.  Basically the only roll our controller will do is house all our potential global instances in one place for us to access to our hearts content.  We have kept this class static so never have to create or depend on any instances of it - this keeps our code clean.

// Here we set our instances into the container 
// class which houses all our potential globals class InC_Controller {
// lets define the keywords for the
// instances we want to hold public static $database; public static $Session; public static $User; // returns a dependency over static method public static function getDependency($dependency) { return self::$$dependency; } }

Next for the purposes of this tutorial, we are going to create a User class, this will house user info.  We have set a property called $this->info to hold “User Profile for John”.  We will then assign an instance of User directly to our controller object above.


// Our User Class class User { private $info = 'User Profile for John'; function getUserInfo() { return $this->info; } function setUserInfo($i) { $this->info = $i; } }

Here we assign our User instance to the controller.  Next we are going to access the setUserInfo method of our User instance via a static method of our Controller object so we can change the User profile from John to Brian.  If we call on getUserInfo, we can print the output to screen, this should display “User Profile for Brian”. 


// Next we assign our User Instance to our controller InC_Controller::$User = new User();
// We can now access our User methods
// directly via our Controller service controller InC_Controller::getDependancy('User')->setUserInfo('User Profile for Brian'); echo InC_Controller::getDependancy('User')->getUserInfo();
 

In our final demonstration of dependency injection with Service Locator, we are going to access our controller from within another class using the service locator, replacing the need to use a global.  First we create a fictitious class called cool, and a method called getCoolUser.  Upon creating an instance of cool, and then calling upon the method getCoolUser, we can output “User Profile for Brain” to the screen, thus saving having to manage and declare globals for instances we may need to use here and there.  Uses for this include instances of database and session objects.

class cool {

    public function getCoolUser() {
// We can also access our User instance
// methods from within other classes,
// no need for Globals!
 return InC_Controller::getDependancy('User')->getUserInfo(); } } // Outputs User Profile for Brian $cool = new cool(); echo $cool->getCoolUser();
  1. cyb3rwizard reblogged this from instantcart
  2. instantcart posted this