Created
February 21, 2010 18:00
-
-
Save shupp/310433 to your computer and use it in GitHub Desktop.
Dependecy Injection Example1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WRONG | |
class Foo1 | |
{ | |
public function bar() | |
{ | |
$cache = Cache::singleton('Memcached'); | |
$result = $cache->get('keyname'); | |
// do some stuff with the result | |
} | |
} | |
// CORRECT | |
class Foo2 | |
{ | |
protected $cache = null; | |
public function bar() | |
{ | |
$cache = $this->getCache(); | |
$result = $cache->get('keyname'); | |
// do some stuff with the result | |
} | |
protected function getCache() | |
{ | |
if ($this->cache === null) { | |
$this->cache = Cache::singleton('Memcached'); | |
} | |
return $this->cache; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment