Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I keep variables used only in one class method local or declare them as class properties?

Tags:

oop

php

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?

Thanks

like image 691
Tom Avatar asked Mar 16 '12 15:03

Tom


People also ask

Are local variables accessible to any method in the class?

The local variable is only active and returns within the method in which it was declared. A local variable is not visible to any other method besides the one in which it exists. If you have a variable that will be used in multiple classes, you must make sure it is outside the method.

What kind of variable do you use if you need to share a variable from one instance of a class to the next?

You need to make the variables in class aaa as class variables, and then you can use these variables of class aaa in class bbb by using object of class aaa. e.g. aaa obj2=new aaa(); System.

Can we declare a variable inside a class?

Variable defined inside the class:The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example – self. var_name.

How do you declare a class variable?

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it.


2 Answers

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.

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).

like image 193
Matt Gibson Avatar answered Oct 07 '22 16:10

Matt Gibson


These local variables are not properties of your object.

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

like image 24
Luc M Avatar answered Oct 07 '22 17:10

Luc M