Last active
December 14, 2015 10:09
-
-
Save rsky/5069716 to your computer and use it in GitHub Desktop.
ミックスインでAOPもどき(オレオレ改造PHPを使用)。
AOPサポートを追加すればInterceptor::invoke()の定義が不要になる。
This file contains hidden or 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
実行例 | |
/opt/php/exam/qiq/bin/php main.php | |
(before) | |
Hello! | |
(after) |
This file contains hidden or 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 | |
trait Interceptor | |
{ | |
public function before() | |
{ | |
echo "(before)\n"; | |
} | |
public function after() | |
{ | |
echo "(after)\n"; | |
} | |
public function invoke() | |
{ | |
$this->before(); | |
parent::invoke(); | |
$this->after(); | |
} | |
} |
This file contains hidden or 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 | |
set_include_path(__DIR__); | |
spl_autoload_register(); | |
function getInterceptor($class) | |
{ | |
return 'Interceptor'; | |
} | |
function getObject($class) | |
{ | |
$interceptor = getInterceptor($class); | |
if ($interceptor) { | |
return new $class use $interceptor; | |
} | |
return new $class; | |
} | |
getObject('Subject')->invoke(); |
This file contains hidden or 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 | |
class Subject | |
{ | |
public function invoke() | |
{ | |
echo "Hello!\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これがいいですね!インターセプターを使って(=use =織り込んで) $classが生まれてる感じがします。