2013-02-14 69 views
1

我是OOP的新用户,请耐心等待我);如何使用依赖注入?

随时发表评论代码。

我有一个RegisterFormParameterHandler类,它扩展了FormParameterHandler。我正在使用它来为注册或登录用户验证$ _POST变量。类'通知'用于错误报告和日志。

我通过传递对象$ notice作为RegisterFormParameterHandler中构造函数的参数来获得代码。我应该在课堂通知中使用静态方法吗?

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'); 
     } 
    } 
} 

这是我如何传递的$ arg的方法validate_something或者是有办法做到这一点以正确的方式一个构造函数?

类通知在类RegisterFormParameterHandler之前使用自动加载器实例化。

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

因此,类通知allready包含一些错误消息,并在调用此类之后使用。

在类RegisterFormParameterHandler中有没有更好的方法来使用类通知?

+0

我不知道在哪里的静态方法来为你的问题,但使用依赖注入你的通知对象放入RegisterFormParameterHandler类是很好的做法;尽管您可能希望使RegisterFormParameterHandler中的$ notice属性受保护,而不是公开 – 2013-02-14 00:22:50

回答

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()方法。我使用call_user_func_array()这里代理的参数传递给其他的验证方法,让你可以得到一些IDE代码洞察善良:

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

code_review-> true。谢谢你的努力! – troks 2013-02-14 01:01:38