Class attribute does not generate error when commented

Asked

Viewed 66 times

5

I had a question that I could not solve. Follow the codes of 3 files

Php user.

class Usuario{
    //protected $nome;

    public function getNome() {
        return $this->nome;
    }

    public function setNome($nome){
        $this->nome = $nome;
    }
}

Pupil.php

class Aluno extends Usuario{

}

index php.

$aluno = new Aluno();

$aluno->setNome($_POST['nome']);          

echo $aluno->getNome();

As you can see, the $name attribute is commented and the line $aluno->setNome($_POST['nome']); returns the value of the field filled in the form. I found it strange, because I thought I had to see an indefinite variable message, or something like.

My question that remained was the following, as the name of the student prints on the screen normally if the attribute $nome is commented?

The connections and other things are just right, I just didn’t put it all in so it wouldn’t get too big.

3 answers

4

I don’t think anyone can explain why designers of the language have chosen to do this in addition to themselves, but I know that they have resolved that if you refer to a member that does not exist this should be automatically created in a public way.

This is a terrible thing a language can do, hiding mistakes. Many people will say that this is one of the reasons PHP is bad and does not even have the excuse of Javascript that made the mistakes in the beginning because it was created in 2 months. Are mistakes made throughout the process over more than 20 years.

If you want this security then you should choose another language.

See how the member is actually created.

I don’t like the term attribute, I prefer field.

3


This is due to PHP allowing to be create class members dynamically at runtime, where you can avoid this using the magic method __set, as in the following example:

<?php

class Usuario
{
  //protected $nome;

  public function getNome()
  {
    return $this->nome;
  }

  public function setNome($nome)
  {
    $this->nome = $nome;
  }

  public function __set($name, $value)
  {}

}

__set: This magic method runs every time it tries to define data to an undefined member in a class.

  • Does not solve the scope problem if the variable has not previously been initialized.

2

As stated before, PHP creates members dynamically, if it does not exist and is called, it is simply created, but if it already exists, it returns the value of that property. When using classes, if the property has not been previously "initialized", it is automatically created as public.

Class properties must be defined as public, private, or protected. If declare using var, the Property will be defined as public.

Below an example:

<?php

class AlunoA
{

    protected $aluno;

    public function getNome(){
        return $this->aluno;
    }
    public function setNome($nome){
        $this->aluno = $nome;
    }

}
class AlunoB
{

    //public $aluno; # comentado ou não é o mesmo nessa situação

    public function getNome(){
        return $this->aluno;
    }
    public function setNome($nome){
        $this->aluno = $nome;
    }

}

$alunoA = new AlunoA();
$alunoB = new AlunoB();
var_dump($alunoA); # object(AlunoA)#1 (1) { ["aluno":protected]=> NULL } 
print "<br/>";
var_dump($alunoB); # object(AlunoB)#2 (0) { } 

?>

For class AlunoB, the student property becomes visible, inside and outside the class, that is, one can access this property through the $this, or through an instance you can read/change the value of $aluno, or directly read its value, which does not guarantee that the integrity of that object is maintained.

$alunoA = new AlunoA();
$alunoB = new AlunoB();
var_dump($alunoA); # object(AlunoA)#1 (1) { ["aluno":protected]=> NULL } 
print "<br/>";
$alunoB->aluno = 'Edilson'; 
var_dump($alunoB); # object(AlunoB)#2 (1) { ["aluno"]=> string(7) "Edilson" }

You can get more details here.

Browser other questions tagged

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