简体   繁体   English

(PHP)是否应该在类中创建“触发器”以访问受保护的/私有功能?

[英](PHP) Should I create “triggers” in classes to access protected/private function?

I didn't find an answer to this question no where, so here it goes: 我没有找到关于这个问题的答案,没有答案,所以去了:

I normally create a protected/private function and public function as well to access the protected/private as a "trigger", is this a good practise or just a pointless excess of code? 我通常会创建一个受保护的/私有功能和公共功能,并以“触发方式”访问受保护的/私有功能,这是一种好习惯还是只是毫无意义的代码?

Here is an example of what I'm talking about... 这是我在说的一个例子...

public function addData($data_c, $data_a)
{
    if ($this->isUser()) {
        $this->addDataDB($data_c, $data_a);
    } else {
        die;
    }
}

private function addDataDB($data_c, $data_a)
{
    $connect = self::connect_data();
    $sql = "INSERT INTO `accounts`(...) VALUES (...)";

    $s_network = $data_c['s_network'];
    $country = $data_c['country'];
    $group_name = $data_c['group_name'];

    foreach ($data_a as $login_password) {

        $account = explode(':', $login_password);

        if (isset($account[0]) && !empty($account[0]) && isset($account[1]) && !empty($account[1])) {

            $login = $this->encryptData($account[0]);
            $password = $this->encryptData($account[1]);

            if (!$this->checkDuplicates($login)) {
                if ($stmt = $connect->prepare($sql)) {
                    $stmt->bind_param("sssssss", ...);
                    $stmt->execute();
                }
                $stmt->close();
            }
        }
    }
    $connect->close();
}

Thats not a bad idea, but the better way would be to create a new decorator class, which handles the secure access. 那不是一个坏主意,但是更好的方法是创建一个新的装饰器类,该类处理安全访问。 In addition, it's a bad idea, to die - instead, you should throw an exception. 另外, die是一个坏主意-相反,您应该抛出异常。

class A {
    function addData(...) {
        // ...
    }
}

class SecureA extends A {
    function addData {
        if (...) {
            throw new NotAllowedException(...);
        }
        parent::addData(...);
    }
}

If you want to go a step further and make you code more cleaner, you should use an interface and don't extend from class A 如果要更进一步,使代码更清晰,则应使用接口,并且不要从A类扩展

interface InterfaceA {
    function addData(...);
}

class A implements InterfaceA {
    function addData(...) {
        // ...
    }
}

class SecureAccessA implements InterfaceA {

    /**
     * @var InterfaceA
     */
    private $a;

    public function __construct(InterfaceA $a) {
        $this->a = $a;
    }

    function addData(...) {
        if (...) {
            throw new NotAllowedException(...);
        }
        $this->a->addData(...);
    }
}

Doing this forces you to modify SecureAccessA , if you change the interface of InterfaceA . 如果更改InterfaceA的接口,则这样做会强制您修改SecureAccessA So you can't silently add functions to A , which are allowed to call, because you forgot to override them in the child class. 因此,您不能向A静默添加允许调用的函数,因为您忘记了在子类中重写它们。

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

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