2013-02-14 4 views
1

OOP를 처음 사용하므로 나와 함께 해주십시오.)Dependency Injection을 사용하는 방법?

코드에 자유롭게 댓글을 남길 수 있습니다.

FormParameterHandler를 확장하는 RegisterFormParameterHandler 클래스가 있습니다. 저는 register 또는 login 사용자를 위해 $ _POST 변수의 유효성을 검사하는 데이 도구를 사용하고 있습니다. 클래스 '통지'는 오류보고 및 로그 용입니다.

registerFormParameterHandler에서 생성자의 인수로 $ notice 개체를 전달하여 코드를 작동 시켰습니다. 수업 공지에서 정적 방법을 사용해야하고 어떻게해야합니까?

class notice { 

private $_notice = array(); 

public function get_notice(){ 
    return $this->_notice; 
} 

public function add($type, $message) { 
    $this->_notice[$type][] = $message; 
} 
} 

과 :

abstract class FormParameterHandler { 

protected $parameters; 

public function __construct($associative_array) { 

    $this->parameters = array(); 

    foreach($associative_array as $key => $value) { 
     $this->{$key} = $value; 
    } 
} 

public function __get($key) { 
    $value = null; 

    if(method_exists($this, "get_$key")) { 
     $value = $this->{"get_$key"}(); 
    } else { 
     $value = $this->parameters[$key]; 
    } 

    return $value; 
} 

public function __set($key, $value) { 
     $value = addslashes($value); 
     $value = htmlentities($value); 

    if(method_exists($this, "set_$key")) { 
     $this->{"set_$key"}($value); 
    } else { 
     $this->parameters[$key] = $value; 
    } 
} 

과 :

class RegisterFormParameterHandler extends FormParameterHandler { 

protected $notice; 

public function __construct($form_parameters, $notice, $tok_id, $captcha) { 
    parent::__construct($form_parameters); 
    $this->notice = $notice; 

    $args = func_get_args(); 

    foreach($form_parameters as $key=>$value) { 
     $key = 'validate_'.$key; 

     $this->$key($args); 
    } 
} 

public function validate_something($args) { 
    if(something === true) { 
     $this->notice->add('error', 'Error message'); 
     } 
    } 
} 

이 나는 ​​방법 validate_something에 $에 인수를 전달하고있어 또는 그렇게 할 수있는 방법이 얼마나 올바른 방법입니다 생성자?

클래스 통지는 RegisterFormParameterHandler 클래스 앞에 자동 로더로 인스턴스화됩니다.

$notice = new notice(); 
    ..... 
    $reg = new RegisterFormParameterHandler($_POST, $notice, $tok_id, $captcha); 

클래스 통지에 이미 일부 오류 메시지가 포함되어 있으며이 클래스가 호출 된 후 사용됩니다.

RegisterFormParameterHandler 클래스에서 클래스 고지를 사용하는 더 좋은 방법이 있습니까?

+0

잘 모르겠어요 정적 방법은 귀하의 질문에 와서, 그러나 의존성 삽입 (Dependency Injection)을 사용하여 통지 객체를 RegisterFormParameterHandler 클래스에 추가하는 것이 좋습니다. RegisterFormParameterHandler의 $ notice 속성을 공개 –

답변

1

이 코드 리뷰 질문 더,하지만 난이 코드를 통해 걷는 동안 몇 가지 작은 변화를 도입하여 대답하려고합니다 :

public function __construct($associative_array) 
{ 
    $this->parameters = array(); 

    foreach($associative_array as $key => $value) { 
     $this->{$key} = $value; 
    } 
} 

당신이 특성을 모방 할 수 있기 때문, 일반적으로 필요하지 않습니다 이미의 구현이있는 __get()__set()를 사용하여 :

public function __construct($associative_array) 
{ 
    $this->parameters = array(); 
} 

것은 나는 당신의 마법 __set 방법에 터치 싶습니다

public function __set($key, $value) 
{ 
    $value = addslashes($value); 
    $value = htmlentities($value); 

    if(method_exists($this, "set_$key")) { 
     $this->{"set_$key"}($value); 
    } else { 
     $this->parameters[$key] = $value; 
    } 
} 

이유가 addslashes()htmlentities() 인 이유는 무엇입니까? 탈출은 수업의 관심사가 아니기 때문에 그곳에 있으면 안됩니다.

RegisterFormParameterHandler의 생성자에.

public function __construct($form_parameters, $notice, $tok_id, $captcha) 
{ 
    parent::__construct($form_parameters); 
    $this->notice = $notice; 

    $args = func_get_args(); 

    foreach($form_parameters as $key=>$value) { 
     $key = 'validate_'.$key; 

     $this->$key($args); 
    } 
} 

첫째, 세 개 이상의 생성자의 매개 변수와 별도의 validate() 방법을 소개하면 대부분이 바로 필요하지 않습니다.

은의이 validate() 방법을 완전히 생성자를 제거하고 쓰기 보자
final public function validate($notice, $tok_id, $captcha) 
{ 
    foreach ($this->parameters as $key=>$value) { 
     call_user_func_array(array($this, "validate_$key"), func_get_args()); 
    } 
} 

지금, $notice에 대한 종속성과 다른 두 개의 인수 만 validate() 방법에 지역이다.나는 당신이 어떤 IDE 코드 통찰력 선량 얻을 수 있도록, 다른 유효성 검사 방법에 프록시 여기에 인수를 call_user_func_array()을 사용하고 있습니다 :

public function validate_something(notice $notice, $tok_id, $captcha) 
{ 
    if(something === true) { 
     $notice->add('error', 'Error message'); 
    } 
} 
+0

code_review-> true가 아닌 protected로 설정하려고 할 수도 있습니다. 노력해 주셔서 감사합니다! – troks

관련 문제