Class attribute does not generate error when commented

5

I had a question I could not solve. Here are the 3 file codes

User.php

class Usuario{
    //protected $nome;

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

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

Student.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 out and the line $aluno->setNome($_POST['nome']); returns the value of the field filled in on the form. I found this strange because I thought I had to get an undefined variable message or something.

My question is the following, how does the student's name print on the screen normally if the $nome attribute is commented out?

Connections and other things more are right, just do not put everything to not get too big.

    
asked by anonymous 21.11.2015 / 16:14

3 answers

3

This is due to PHP allowing to be create class members dynamically at runtime, where you can avoid this by using the __ set magic method, 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.

    
21.11.2015 / 17:00
4

I can not explain why the designers of the language chose to do this, but I know that they have resolved that if you refer to a member that does not exist, it must be automatically created publicly. / p>

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

See how the member is actually created .

I do not like the term attribute, I prefer field .

    
21.11.2015 / 16:46
2

As I said 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 you declare using var, the property will be defined as public.

Below is 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 the AlunoB class, the student property becomes visible, inside and outside the class, that is, you can access this property through $this , or through an instance you can read / change the value of $aluno , or read its value directly, 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 .

    
21.11.2015 / 17:28