Can I use a variable to override a method?

3

When I rewrite a method, in PHP, can I use a variable to receive the method?

I made the example below and it worked normally, but I do not know if this is correct or if it is the best way to rewrite a parent class method.

abstract class Animal{
  ...
  public function dadosAnimal(){
    $dados  = " Nome: ". $this->nome;
    $dados .= "Idade: ". $this->idade;

    return $dados;
  }

class Cachorro extends Animal(){
   ...
   public function dadosAnimal(){
     //Posso fazer ou vai contra algum principio ou patter ?
     $dados = parent:: dadosAnimal();
     $dados .= " Cor do pelo: ". $this->corPelo;

     return $dados;
}

What I want is not to have to repeat the same parent class code in the child classes and also have the benefit of changing something just in the parent class and the change is reflected for all child classes that use the parent class.

Avoid this:

public function dadosAnimal(){
    parent::dadosAnimal();
    //Copiar o método inteiro da classe-pai
    $dados = "<br/> Nome: ". $this->nome;
    $dados .= "<br/> Idade: ". $this->idade;
    //reescrever, adicionando isso
    $dados .= "<br/> Cor do pêlo: ". $this->corPelo;

    return $dados;
    
asked by anonymous 28.12.2014 / 21:59

2 answers

3

This is correct. In several cases we just want to add a behavior to an inherited method. That is, we want, in addition to the behavior of the parent class, a new action is taken. In these cases we call the parent method using the reference parent:: .

In your case, the behavior of the parent method is to return a description of Animal , and the behavior you want to add is to return information from the hair, related to the Cachorro class, which extends the Animal class %.

There could be a problem if you rewrite the Animal::dadosAnimal() command in your Cachorro class as follows:

public function dadosAnimal(){
    $dados  = " Nome: ". $this->nome;
    $dados .= "Idade: ". $this->idade;
    $dados .= " Cor do pelo: ". $this->corPelo;
    return $dados;
}

If your intention was to completely hide the behavior of the Animal class in this method, implementing this would be a problem. Imagine the following scenario: In addition to the Cachorro class, you have Gato , Passaro , etc. You write the classes, replicating the behavior of the Animal class as above.

After a while you decide that you no longer want the "name" to appear in your dadosAnimal() method. What do you do? Scroll through all classes by removing the "name" part. Would not it be much easier if you could remove it in one place and all the others automatically have their names removed?

This is an example of code reuse, and is derived from the way you wrote it, which is therefore the indicated way if you do not want to hide the behavior of a method of a hierarchically preceding class.

Note that on line $dados = parent:: dadosAnimal(); you are not assigning a method to $dados , but rather assigning the result of the parent::dadosAnimal() method to it. To assign a method, you should not make the call by hiding the parentheses () . This way, it would be wrong in this context.

    
28.12.2014 / 22:15
4

There's no problem doing this.

But your question does not seem to make much sense. I think you think you're doing something you're not. If I understood what you meant.

  

Can I use a varable to override a method?

This is simply not possible. So much so that you did not do this.

Variables are used to name a memory location that will store data. Now, if a method returns data, a variable naming a memory location where these data are is normal. Do not interpret the concept of a variable more than this. Of course there are some details that do not come to the case now but variable is just this.

We can roughly say that a variable holds data. This is a wrong concept but it does not matter much to who is starting. In case you are just saving data returned by the method, you are not overwriting it. It is not changing anything defined in the upper class, it is not complicating object orientation, in fact it is not doing anything that is abnormal.

I would change this code a bit, I hate creating variables when pure data can be used without problems (some think differently):

abstract class Animal {
  ...
  public function dadosAnimal() {
    return " Nome: " . $this->nome . "Idade: " . $this->idade;
  }

class Cachorro extends Animal() {
   ...
   public function dadosAnimal() {
     return parent::dadosAnimal() . " Cor do pelo: " . $this->corPelo;
}
    
28.12.2014 / 22:15