의존성 주입에 대해 많이 읽으므로 지금 뭔가를 만들고 있습니다. 간단한 양식 제출을 생각했습니다. 기본적으로 제목은 input
필드이고 본문은 textarea
인 양식입니다. 그런 다음컨테이너를 사용한 의존성 주입
나는이처럼 용기를 가지고 :
class IoC
{
protected $db;
public static function newPost()
{
$post = new Post(); // Instantiate post class so we can use the methods in there
$input = $post->getInput(); // Method that gets the POST values
$post->insertInput($input, $db); // Method that adds the post values to a database
}
}
//Call IoC::newPost(); on the page the form submits to
이
는Post
클래스 :
class Post
{
protected $db;
public function __construct($db)
{
$this->db = $db;
}
public function getInput()
{
// Should I get the post input here? Like $_POST['title'] etc. and put it
// into an array and then return it?
return $input;
}
public function insertIntoDB($db, $input)
{
// Should I hardcode the connection and query here?
}
}
당신이 볼 수 있듯이, 나는 연결에서 온해야하는 위치에 같은 혼란 스러워요 . 그것에 대해 생각해 보면 연결을 생성하고 해당 클래스를 컨테이너에서 호출하는 별도의 재사용 가능한 Database
클래스를 갖는 것이 현명 할 것이라고 생각하십니까?
나는 정말로 모른다. 당신이 어떻게 할 것인지 당신에게 이야기하고, 당신에게 어떤 사례가 있다면 그것을 말해 보라.