繁体   English   中英

如何使用依赖注入?

[英]How to use Dependency Injection?

我是OOP的新手,所以请耐心等待我;)

随意注释代码。

我有一个扩展FormParameterHandler的类RegisterFormParameterHandler。 我正在使用它来验证注册用户或登录用户的$ _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');
        }
    }
}

这是我在方法validate_something中传递$ arg的正确方法,还是有一种在构造函数中执行此操作的方法?

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

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

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

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

这更多是一个代码审查问题,但是我将通过在遍历代码时引入一些小的更改来尝试回答这个问题:

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您的魔术__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()将参数代理到其他validate方法,以便您可以获得一些IDE代码见解的优点:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM