我应该只在一个类方法中使用变量,还是将它们声明为类属性?

时间:2022-09-20 10:34:47

I've been wondering if a class property is instantiated and used only in one class method should it be a class property at all or should it just be a local variable accessible to that class method only?

我一直在想,一个类属性是否被实例化并只在一个类方法中使用它应该是一个类属性,还是仅仅是一个只能被那个类方法访问的局部变量?

For example, should I keep a variable only used in one method as a local variable like this:

例如,我是否应该只在一种方法中使用一个变量作为局部变量:

class myClass
{
    public function myMethod()
    {
        $_myVariableUsedOnlyOnce = "Hello World";
        echo $_myVariableUsedOnlyOnce;
    }
}

Or should I make the variable a private class property like this:

或者我应该将变量设置为如下所示的私有类属性:

class myClass
{
    private $_myVariableUsedOnlyOnce;

    public function myMethod()
    {
        $this->_myVariableUsedOnlyOnce = "Hello World";
        echo $this->_myVariableUsedOnlyOnce;
    }
}

Which approach "smells"? What are the benefits to making all method variables class properties other than when I need to print_r() the entire object for debugging purposes?

哪种方法“气味”?除了需要将整个对象print_r()用于调试目的之外,使所有方法变量类属性都有什么好处?

Thanks

谢谢

5 个解决方案

#1


4  

If you need it to have persistence across function calls, a class property would be best so that it moves around as the object does. You also might want to use it for other reasons in future in other functions. However, it does add overhead.

如果您需要它具有跨函数调用的持久性,那么类属性将是最好的,以便它像对象那样移动。您还可能希望在其他函数中使用它。但是,它确实增加了开销。

Generally, the class should have some real-world analogue, so if your variable corresponds to something that makes sense e.g. a person class has a $height, then it belongs as a class property. Otherwise, if it's just a part of the internal calculations of a method, then it doesn't really belong attached to the class e.g. a person does not have a $shoelaceIterator or whatever.

通常,类应该有一些真实的类比,所以如果您的变量对应于一些有意义的东西,例如person类有一个$height,那么它就属于类属性。否则,如果它只是一种方法的内部计算的一部分,那么它就不属于这个类,比如一个人没有一个$shoelaceIterator之类的东西。

I'd argue that a confusing object design would be more of a smell than a potentially small memory overhead (although this depends on how big the variable is).

我认为,令人困惑的对象设计更多的是一种气味,而不是潜在的小内存开销(尽管这取决于变量的大小)。

#2


1  

These local variables are not properties of your object.

这些局部变量不是对象的属性。

They are not defining your object, then they should not be declared as private member.

它们没有定义您的对象,那么它们不应该被声明为私有成员。

#3


0  

First I would ask if you really need the variable/property at all if you are only using it once. As for which one "smells", a property is stored in memory for the entire life of the object whereas the variable is only in memory until the method finishes executing.

首先,我想问你是否真的需要这个变量/属性如果你只用它一次。至于哪一个“气味”,一个属性存储在对象的整个生命周期中,而变量仅在内存中,直到方法完成执行。

#4


0  

If you don't need a variable outside the method, it should not be any property of the class. Moreover, accessing local variables is faster.

如果在方法之外不需要变量,那么它不应该是类的任何属性。此外,访问本地变量更快。

#5


0  

In a pure design approach I would suggest you to make your choice according to what the attribute/property is supposed to model.

在纯设计方法中,我建议您根据应该建模的属性/属性进行选择。

In pure performance terms, having one static attribute is better because memory space won't be allocate with each instance of the class.

就性能而言,拥有一个静态属性会更好,因为不会为类的每个实例分配内存空间。

#1


4  

If you need it to have persistence across function calls, a class property would be best so that it moves around as the object does. You also might want to use it for other reasons in future in other functions. However, it does add overhead.

如果您需要它具有跨函数调用的持久性,那么类属性将是最好的,以便它像对象那样移动。您还可能希望在其他函数中使用它。但是,它确实增加了开销。

Generally, the class should have some real-world analogue, so if your variable corresponds to something that makes sense e.g. a person class has a $height, then it belongs as a class property. Otherwise, if it's just a part of the internal calculations of a method, then it doesn't really belong attached to the class e.g. a person does not have a $shoelaceIterator or whatever.

通常,类应该有一些真实的类比,所以如果您的变量对应于一些有意义的东西,例如person类有一个$height,那么它就属于类属性。否则,如果它只是一种方法的内部计算的一部分,那么它就不属于这个类,比如一个人没有一个$shoelaceIterator之类的东西。

I'd argue that a confusing object design would be more of a smell than a potentially small memory overhead (although this depends on how big the variable is).

我认为,令人困惑的对象设计更多的是一种气味,而不是潜在的小内存开销(尽管这取决于变量的大小)。

#2


1  

These local variables are not properties of your object.

这些局部变量不是对象的属性。

They are not defining your object, then they should not be declared as private member.

它们没有定义您的对象,那么它们不应该被声明为私有成员。

#3


0  

First I would ask if you really need the variable/property at all if you are only using it once. As for which one "smells", a property is stored in memory for the entire life of the object whereas the variable is only in memory until the method finishes executing.

首先,我想问你是否真的需要这个变量/属性如果你只用它一次。至于哪一个“气味”,一个属性存储在对象的整个生命周期中,而变量仅在内存中,直到方法完成执行。

#4


0  

If you don't need a variable outside the method, it should not be any property of the class. Moreover, accessing local variables is faster.

如果在方法之外不需要变量,那么它不应该是类的任何属性。此外,访问本地变量更快。

#5


0  

In a pure design approach I would suggest you to make your choice according to what the attribute/property is supposed to model.

在纯设计方法中,我建议您根据应该建模的属性/属性进行选择。

In pure performance terms, having one static attribute is better because memory space won't be allocate with each instance of the class.

就性能而言,拥有一个静态属性会更好,因为不会为类的每个实例分配内存空间。