DI的基本形式:
class TestClass
{
private $obj;
public function __constructor($obj)
{
$this->obj = $obj;
}
}
但如果我必须传递20个对象,来自同一个来源并且我不知道将使用哪个对象怎么办?那么,它将是一个数据库模型:
class Testt
{
private $forum, $forumThread, $users, $posts;
public function __constructor($forum, $forumThread, $users, $posts)
{
$this->forum = $forum;
$this->forumThread = $forumThread;
$this->users = $users;
$this->posts = $posts;
}
}
当新模型出现时,此代码会发生变化。更不用说是否有更多的Test *类。但是,如果我做了
class Models
{
private $forum, $forumThread, $users, $posts;
public function __constructor($forum, $forumThread, $users, $posts)
{
$this->forum = $forum;
$this->forumThread = $forumThread;
$this->users = $users;
$this->posts = $posts;
}
}
class Test
{
private $models;
public function __constructor(Model $models)
{
$this->models = $models;
}
}
这将是最好的,不是吗?可悲的是,我还没有看到任何一篇关于课程的文章。