It is wrong to use common variables within a class?
It’s not about being wrong, it’s just not allowed.
It is an erroneous form of POO?
What? Using variables that look like locations in the class? This is not possible as stated before. Or are you talking about variables in methods as part of the question?
If so, it has nothing to do with object orientation. Class variables can be related to OOP. So it is not possible to say if the use in methods is right or wrong thinking about OOP.
The fact is that variables should have the scope as restricted as possible always. If every programmer learned this the codes would already be much better.
Can I only use attributes within my class? Can I use type variables $Variavel
, when necessary?
Outside the method, but within the class there has to be an indicator that that is a variable using one of the visibility modifiers public
, private
, protected
, or the obsolete var
, besides the static
. So in class you can only do something like this:
public $var1;
private $var2;
protected var3;
static $var4;
I mean, don’t use the public $variavel
, or var $variavel
, private $variavel
.
Can’t.
I’m talking about using common variables, just the $variavel
within certain methods, when necessary.
Now we’re talking about something else. Here the question changes from class variables to method variables. You need to conceptualize correctly. Without understanding the concept becomes difficult to do anything right.
Power, it 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 cannot declare variables within methods with the quoted modifiers. These visibility modifiers only make sense in class variables.
Local variables are deprived of the method always, so they are called "places", their access is very restricted to the place where it exists. Its purpose is to save state to the algorithm internal to the method. It works the same as any function. They have nothing to do with instance variables or purely class variables (static).
A parameter is still a local variable.
But you can access the class members. If the word "use" is written that way, then you can. Remember that to access class variables you are actually accessing the variable $this
. What many people don’t know is that a method always has a hidden parameter called $this
, so you can access the class variables, in the background you are receiving the object in this variable and then access its members through it.
I’m asking if I can use normal/local variables like $var
, without public
, private
, var
or any access modifier, in class methods
As said before, the word "use" is ambiguous. Create (declare) can only variables like this. Access or modify the value can do also members declared in class.
A simplified example (it is not appropriate 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
}
}
I put in the Github for future reference.
Set an example for us to understand better what you are talking about. I mean, without
var
,public
,private
, nothingness?– Maniero
About "being wrong" already I say, the only thing wrong even in programming, is to use things without knowing. If you know what you’re doing, you can mix POO with procedural, mix C with C++ and commit the "sacrilege" of not using
.h
plus, use ISO-8859-1 when it is most convenient, use agoto
well positioned or a Return in the middle of the function, and so on. It would be nice if you [Dit] the question and put an example, and better describe what the doubt is, so the staff can give technical answers, if there are real advantages, real disadvantages, or if it is the same thing.– Bacco
Using/declaring local variables in methods is no problem and is not wrong.
– rray
Part of the question talks about class attributes, part talks about method variables, which are effectively talking about?
– Maniero