It is wrong to use common variables within a PHP class

1

Is it an erroneous form of OOP? Can I use only attributes within my class? Can I use variables of type $Variavel , when needed?

I mean, do not use public $variavel , or var $variavel , private $variavel .

I'm talking about using common variables, only $variavel within some methods, when necessary.

I am asking if I can use normal / local variables of type $var , without public , private , var or any access modifier, in methods of a class.     

asked by anonymous 05.10.2016 / 17:18

2 answers

1

There are no problems and it's not wrong either.

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

Example of something not smart:

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 the X () method there is a local variable that received an information, it read a file.

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

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

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 does not need to be concerned if the information has been modified in the meantime between the methods call.

Basically just pay attention to redundancies.

However, if you are trying to say something like using the variables inside the class and outside the body of the methods, then it does not make sense since only declarations 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.

    
05.10.2016 / 17:58
2
  

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
    }
}
    
05.10.2016 / 17:56