Skip to content

Instantly share code, notes, and snippets.

@crynobone
Created December 2, 2013 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crynobone/7746273 to your computer and use it in GitHub Desktop.
Save crynobone/7746273 to your computer and use it in GitHub Desktop.
<?php
class EmailNotifier implements NotifierInterface {
public function send()
{
// send email.
}
}
<?php
interface NotifierInterface {
public function send();
}
<?php
class User {
protected $notifier;
public function __construct(NotifierInterface $notifier)
{
$this->notifier = $notifier;
}
public function update()
{
// update user.
$this->notifier->send();
}
}
App::bind('NotifierInterface', 'EmailNotifier');
$user = App::make('User'); /* you can't use new User */
<?php
class User {
protected $notifier;
public function __construct()
{
$this->notifier = App::make('NotifierInterface');
}
public function update()
{
// update user.
$this->notifier->send();
}
}
App::bind('NotifierInterface', 'EmailNotifier');
$user = App::make('User'); /* or new User */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment