It is wrong to use common variables within a PHP class

Asked

Viewed 953 times

-1

Is it a wrong form of POO? Can I only use attributes within my class? Can I use type variables $Variavel, when necessary?

I mean, don’t use the public $variavel, or var $variavel, private $variavel.

I’m talking about using common variables, just the $variavel within certain methods, when necessary.

I’m asking if I can use normal/local variables like $var, without public, private, var or any access modifier, in class methods.

  • 2

    Set an example for us to understand better what you are talking about. I mean, without var, public, private, nothingness?

  • 3

    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 a goto 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.

  • Using/declaring local variables in methods is no problem and is not wrong.

  • 2

    Part of the question talks about class attributes, part talks about method variables, which are effectively talking about?

2 answers

3

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.

-1


There’s no problem and it’s not wrong either.

What can be "wrong" is using local variables for resources that could be shared.

Example of something not at all intelligent:

class foo() {

    private $a;

    private function X() {
        $data = file_get_contents('arquivo.txt');
        //afz uns cambalacho aqui e tal
    }

    private function Y() {
        $data = file_get_contents('arquivo.txt');
        // faz as firulas..
    }
}

$c = new foo();
$c->X();
$c->Y();

This is an example of an erroneous case. Because in method X() there is a local variable that received information, read a file.

In the Y() method there is also the same process. It is redundant and is consuming unnecessary resources.

If you are going to run both methods in the same instance, then the ideal would be to share already processed information.

class foo() {

    private $a;
    private $data = null;

    private function X() {
        $this->D();
        //faz uns cambalacho aqui e tal
    }

    private function Y() {
        $this->D();
        // faz as firulas..
    }
    private function D() {
        if (empty($this->data)) {
            $this->data = file_get_contents('arquivo.txt');
        }
    }
}

$c = new foo();
$c->X();
$c->Y();

Logical, considering that the data in the file is static and need not be concerned if the information has been modified in the meantime between the call of the methods.

Basically just pay attention to redundancies.

However, if you are trying to say something like using variables within the class and outside the body of methods, then it makes no sense, as only statements can be made.

class Foo(){
     $v = 1; //isso é permitido mas em versões recentes exige-se a declaração da visibilidade (public, private, static)

}

*The above examples are purely didactic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.