Last active
February 16, 2017 18:10
-
-
Save TsuiAnthonYVR/f72af41b5b03e69d7193b7b619fbf360 to your computer and use it in GitHub Desktop.
Use decorator without making type hinting mad
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 | |
namespace App; | |
use App\Contracts\MailingListService; | |
abstract class MailingListBaseDecorator implements MailingListService | |
{ | |
protected $mailingList; | |
public function __construct(MailingListService $mailingList = null) { | |
$this->mailingList = $mailingList; | |
} | |
public function addEmailToList(string $list, string $email, string $firstName = '', string $lastName = '') { | |
error_log ('base addEmailToList'); | |
$this->__call(__FUNCTION__, func_get_args()); | |
} | |
public function sendMarketingEmail(string $email, string $firstname, string $lastname = ''){ | |
error_log ('base sendMarketingEmail'); | |
$this->__call(__FUNCTION__, func_get_args()); | |
} | |
public function __call($method, $args) { | |
return $this->mailingList->{$method}(...$args); | |
} | |
} |
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 namespace App\Contracts; | |
interface MailingListService | |
{ | |
public function addEmailToList(string $list, string $email, string $firstName = '', string $lastName = ''); | |
public function sendMarketingEmail(string $email, string $firstname, string $lastname = ''); | |
} // interface MailingListService |
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 | |
namespace App; | |
use App\MailingListBaseDecorator; | |
use App\Contracts\MailingListService; | |
class TestInheriterA extends MailingListBaseDecorator | |
{ | |
public function decoratedFunctionA() { | |
error_log ('decorated functionA'); | |
} | |
public function addEmailToList(string $list, string $email, string $firstName = '', string $lastName = '') { | |
error_log ('overwritten addEmailToList A'); | |
} | |
} |
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 | |
namespace App; | |
use App\MailingListBaseDecorator; | |
use App\Contracts\MailingListService; | |
class TestInheriterB extends MailingListBaseDecorator | |
{ | |
public function decoratedFunctionB() { | |
error_log ('decorated function B'); | |
} | |
public function addEmailToList(string $list, string $email, string $firstName = '', string $lastName = '') { | |
error_log ('overwritten addEmailToList B'); | |
} | |
} |
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
>>> $c = new App\TestInheriterA(new App\TestInheriterB) | |
=> App\TestInheriterA {#1147} | |
>>> $c->decoratedFunctionA() | |
decorated functionA | |
=> null | |
>>> $c->decoratedFunctionB() | |
decorated function B | |
=> null | |
>>> $c->addEmailToList('list','email') | |
overwritten addEmailToList A | |
=> null | |
>>> | |
>>> class_implements($c) | |
=> [ | |
"App\Contracts\MailingListService" => "App\Contracts\MailingListService", | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment