Skip to content

Instantly share code, notes, and snippets.

@shupp
Created February 21, 2010 18:00
Show Gist options
  • Save shupp/310433 to your computer and use it in GitHub Desktop.
Save shupp/310433 to your computer and use it in GitHub Desktop.
Dependecy Injection Example1
<?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