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

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

Tags:

Php

Oop