PHP classes - is it preferable to use regular variables instead of properties?
For my latest website, I am trying to use classes. Basically to learn more OOP and learn from experience.
Whenever I need a variable in my class, I have created a property like:
class someClass
{
var $valueToUseHere; // Only used internally - can I just use a variable?
public function doStuff()
{
$this->valueToUseHere = 60;
// Do more stuff here...
}
}
It was only now that I was looking more code and trying to do some optimization that I noticed that my functions and classes are looping around some large objects. Much of this scope can be removed if I make all the properties that are only used inside the class into regular variables.
Are properties used only for variables that are needed outside the class, and is it then legal to use "ordinary variables inside the class itself?"
Sorry if this question illustrates a lack of understanding on this matter. Unfortunately, I am currently engaged in teaching. I've done some searches around "class properties versus variables" etc. but couldn't find a comprehensive answer to this question.
Many thanks
source to share
It is somewhat vague what you are asking, but if valueToUseHere
not used outside doStuff
then don't make it a property!
class someClass {
public function doStuff() {
$valueToUseHere = 60;
// Do more stuff here...
}
}
If there is no reason to share this value with other class methods or with the outside world, then there is no reason to interfere with your object with all kinds of properties. Not only can this cause complex errors with saved state, it also forces you to be overly careful with variable names in all object methods.
source to share
The class's own properties are also variable. Basically, you have three options:
Global variable available everywhere but not recommended because all parts of your code can depend on such a varialbe, changes can easily damage stuff all over the place.
A class property (Note: you must define visibility - public / protected / private), these properties are bound to an instance of the object and must be used for any state that the object must retain for further processing. They can usually be used in more than one method in your class.
Variables inside the method as soon as
public function doStuff()
{
$valueToUseHere = 60;
// Do more stuff here...
}
The variable is available only inside the method and is discarded at the end of the method execution.
source to share
It depends on your needs. If you just want to store a value in a variable, it's best to keep it simple and not define functions to set or get its value. But sometimes you may need more controls for a variable in your class. For example, you have defined an integer variable and you want the values ββto always be between 10 and 1000, and also should not be in 100 200 300, .., 900. So there is a good reason here to set up access to the variable for private access and create a public function to check what is required before setting a new value. Or, in another example, you can call another function or change another dependent variable in your class exactly after changing that variable. Or, if you want to make the variable read-only or write-only,you can always define properties to control the value of a variable.
In short, you may prefer to use:
Properties . If you want to have a control to get and set the values ββof
Variables: If you want to set or use a variable as your nature
source to share