Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created November 22, 2017 07:09
Show Gist options
  • Select an option

  • Save lastday154/c769252a4c309c7803487ba86d114799 to your computer and use it in GitHub Desktop.

Select an option

Save lastday154/c769252a4c309c7803487ba86d114799 to your computer and use it in GitHub Desktop.
Dependency Injection
<?php
class Database
{
protected $adapter;
public function __construct()
{
$this->adapter = new MysqlAdapter;
}
}
class MysqlAdapter
{
}
// using DI
class Database
{
protected $adapter;
public function __construct(MysqlAdapter $adapter)
{
$this->adapter = $adapter;
}
}
// Using D in SOLID "Depends on Abstraction, not depend on concretions"
class Database
{
protected $adapter;
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
}
interface AdapterInterface {}
class MysqlAdapter implements AdapterInterface
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment