Is it wrong to use common variables within a class?
It's not a matter of being wrong, it's just not allowed.
Is it an erroneous form of OOP?
What? Use variables that looks like places in the class? This is not possible as stated before. Or are you talking about variables in methods like part of the question?
If it is, it has nothing to do with object orientation. Class variables may be related to OOP. So it's not fair to say that the use in methods is right or wrong thinking about OOP.
The fact is that variables should have the tightest scope every time. If every programmer learned that the codes would be much better.
Can I use only attributes within my class? Can I use variables of type $Variavel
, when needed?
Out of method, but within the class there must be an indicator that it is a variable using one of the% modifiers public
, private
, protected
, or the obsolete var
, in addition to static
. So in the class can only something like this:
public $var1;
private $var2;
protected var3;
static $var4;
I mean, do not use public $variavel
, or var $variavel
, private $variavel
.
You can not.
I'm talking about using common variables, only $variavel
within some methods, when necessary.
Now we are talking about something else. Here the question changes from class variables to method variables. You need to conceptualize correctly. Without understanding the concept it becomes difficult to do anything right.
Power, you can. If the algorithm does not require intermediate state within the method, that's fine. Do what you have to do. In documentation most examples do not use local variables.
Within methods you can only create local variables. You can not declare variables within methods with the quoted modifiers. These visibility modifiers only make sense in class variables.
Local variables are always private to the method, so they are called "local", their access is very restricted to the location it exists. Its purpose is to keep state to the algorithm built into the method. It works just like any function. They have nothing to do with instance variables or purely class (static).
A parameter is still a local variable.
But you can access the members of the class. If the word "use" is written in this sense, then it can. Remember that to access the class variables you are actually accessing the $this
variable. What many people do not know is that a method always has a hidden parameter called $this
, so it can access the variables of the class, in the background you are receiving the object in that variable and there access its members through it.
I'm wondering if I can use normal / local variables of type $var
, without public
, private
, var
or any access modifier, in methods of a class
As I said before, the word "use" is ambiguous. Create (declare) can only variables like this. Accessing or modifying the value can also on members declared in the class.
A simplified example (not suitable to do just that):
class Produto {
private $preco; //variável de classe
public function PrecoComDesconto($desconto) {
//variável local - acessa variável de classe e parâmetro
$valorDescontado = $this->preco * (1 + $desconto / 100);
return $valorDescontado < 10 ? 10 : $valorDescontado; //o valor não pode ser < 10
}
}