あなたが参照したブログ投稿が非常に興味深く、洞察に満ちているとは思いませんでした。
あなたが説明していることは、依存性注入に関係する何よりもデコレータのように見えます。依存性注入は、オブジェクトグラフを作成する方法であり、作成された後の状態ではありません。
そうは言っても、デコレータパターンを使用して実行することをお勧めします。
interface PostInterface
{
public function title();
}
class PostModel implements PostInterface
{
public function title()
{
return $this->title;
}
}
class PostViewHelper implements PostInterface
{
public function __construct(PostInterface $post)
{
$this->post = $post;
}
public function title()
{
return $this->post->title();
}
}
class PostFilter implements PostInterface
{
public function __construct(PostInterface $post)
{
$this->post = $post;
}
public function title()
{
return $this->filter($this->post->title());
}
protected function filter($str)
{
return "FILTERED:$str";
}
}
次のように、このオブジェクトグラフを作成するために必要なDIフレームワークを使用するだけです。
$post = new PostFilter(new PostViewHelper($model)));
複雑なネストされたオブジェクトを作成するときに、このアプローチをよく使用します。
発生する可能性のある問題の1つは、で「多すぎる」関数を定義することですPostInterface
。これらをすべてのデコレータクラスに実装しなければならないのは面倒な場合があります。私はこれを回避するためにPHPの魔法の関数を利用しています。
interface PostInterface
{
/**
* Minimal interface. This is the accessor
* for the unique ID of this Post.
*/
public function getId();
}
class SomeDecoratedPost implements PostInterface
{
public function __construct(PostInterface $post)
{
$this->_post = $post;
}
public function getId()
{
return $this->_post->getId();
}
/**
* The following magic functions proxy all
* calls back to the decorated Post
*/
public function __call($name, $arguments)
{
return call_user_func_array(array($this->_post, $name), $arguments);
}
public function __get($name)
{
return $this->_post->get($name);
}
public function __set($name, $value)
{
$this->_post->__set($name, $value);
}
public function __isset($name)
{
return $this->_post->__isset($name);
}
public function __unset($name)
{
$this->_post->__unset($name);
}
}
このタイプのデコレータを使用すると、装飾された機能を提供するために必要な方法を選択的にオーバーライドできます。オーバーライドしないものはすべて、基になるオブジェクトに戻されます。基になるオブジェクトのインターフェイスを維持しながら、複数の装飾をすべて行うことができます。