哪个是初始化php属性的更好方法? [英] Which is the better approach to initialize php properties?

查看:67
本文介绍了哪个是初始化php属性的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种初始化类变量的方法。

Here are two way to initialize class variables.

class Test {
    private $var1;
    private $var2;

    public function Test($var1,$var1) {
        $this->var1 = $var1;
        $this->var2 = $var2;
    }
}
$objTest = new Test("value1","value2");



第二种方法



2nd Method

class Test {
    private $var1;
    private $var2;

    public function _set($var, $value) {
        $this->$$var = $value
    }
}
$objTest = new Test();
$objTest->_set('var1','value1');
$objTest->_set('var2','value2');

现在,这两种方法都是有效的,但是我想知道哪种情况下更好?仅使用一种方法的优点和缺点是什么?

Now, these both methods are valid, but I would like to know which one in better in what conditions? What are pros and cons of sticking with one method only?

推荐答案

在您的示例中,第二种方法具有很高的风险。如果将变量名作为参数,则基本上可以使代码具有从类外部设置所有私有变量的权限。如果让私有变量像这样自由设置,那么拥有私有变量的意义何在?

In your example, the second method is highly risky. If you give the variable name as an argument, you basically give the code the access to set all private variables from outside the class. What is the point of having private variables if you allow them to be set freely like that?

另外,封装在OOP中的一点是,a的内部工作原理类对类外的代码不透明。您的第二种方法破坏了这种封装,从而破坏了OOP的意义,因为类外部的代码必须了解类的内部工作方式,例如变量的名称。如果您以后选择更改变量名称,会发生什么情况?所有代码都中断了。如果通过设置器/获取器访问它们,则可以更改旧函数以反映类内部的更改,但是类外部的代码将很难更改。除此之外,第二种方法使值的验证变得困难。

Additionally, the point of encapsulation in OOP, is that the inner workings of a class are not transparent to the code outside the class. Your second method breaks this encapsulation and thus part of the point of OOP, as the code outside the class has to be aware of the inner workings of the class, like the name of the variables. What happens if you later choose to change the variable names? All the code breaks. If they were accessed via setters/getters, old functions could be changed to reflect changes inside the class, but code outside the class would be difficult to change. In addition to that, the second method makes the validation of the values hard.

您应该使用第一种方法,尤其是在需要设置类变量来进行操作的情况下。但是,如果您认为属性可以使用一些默认值,则可以利用PHP的默认参数值,例如:

You should use the first method, especially if setting the class variables is necessary for operation. However, if you feel that some default values can be allowed for the attributes, you can just take advantage of PHP's default argument values like:

class Test {
    private $var1;
    private $var2;

    public function Test($var1 = 'defaultValue', $var1 = 'defaultValue') {
        $this->var1 = $var1;
        $this->var2 = $var2;
    }
}
$objTest = new Test();

无论如何,如果必须通过代码初始化值,那么您绝对应强制将它们传递在构造函数中。如果允许使用默认值,则可以使用变量的单独的setter初始化构造函数中的值,或者仅像提供的示例中那样使用默认参数值。但是,期望代码在构造函数被调用后通过设置器设置临界值是一种错误的做法。

Anyway, if the values must be initialized by the code, then you should definitely force them to be passed in the constructor. If default values are allowed, then either initialize the values in constructor with separate setters for the variables or just default argument values like in the provided example. It is, however, bad practice to expect the code to set critical values via setters after the constructor has been called.

这篇关于哪个是初始化php属性的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆