什么是更好的方法来初始化类变量? [英] What is the better approach to initialize class variables?

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

问题描述

以下是初始化类别变数的两种方法。

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中的封装点是,类对类外部的代码不透明。你的第二个方法打破了这个封装,因此是OOP的一部分,因为类外面的代码必须知道类的内部工作,就像变量的名称。如果以后选择更改变量名称会发生​​什么?所有代码中断。如果通过setters / getter访问它们,旧的函数可以被改变以反映类中的改变,但是类之外的代码将难以改变。除此之外,第二种方法使得难以验证值。

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.

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

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