Can I use a variable to overwrite a method?

Asked

Viewed 887 times

4

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

I did the example below and it worked normally, but I don’t 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 keep repeating the same father class code in the daughter classes and also have the benefit of changing something only in the parent class and the change is reflected for all the daughter classes that use the parent class method.

Prevent 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;

2 answers

4

It’s okay to do this.

But your question doesn’t seem to make much sense. I think you think you’re doing something you’re not doing. If I understand what you mean.

I can use a variable to overwrite a method?

This is simply not possible. So much so that you didn’t do this.

Variables are used to name a memory position that will store data. Now, if a method returns data, a variable giving name to a memory position where that data is located is normal. Don’t interpret the concept of a variable any more than this. Of course there are some details that are not the case now but variable is just this.

Roughly speaking we can say that a variable stores data. This is a misconception but it doesn’t matter much to those who are starting out. In case you are only storing data returned by the method, you are not overwriting it. You’re not changing anything set in the upper class, you’re not complicating object orientation, you’re not doing anything that’s abnormal.

ME would change this code a little, I hate creating variables when pure data can be used without problems (some people 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;
}

I put in the Github for future reference.

3


Yes. In many 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 to be 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 the Animal, and the behavior you want to add is to return hair information, related to the class Cachorro, extending the class Animal.

There could be a problem in case you rewrite the command Animal::dadosAnimal() in your class Cachorro 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 class Animal in this method, implementing in this way would be a problem. Imagine the following scenario: beyond the class Cachorro, you own Gato, Passaro, etc. You write classes, replicating class behavior Animal like the example above.

After a while you decide that you no longer want the "name" to be displayed in your method dadosAnimal(). What do you do? Scroll through all classes by removing the "name" part. It wouldn’t be much easier if you could remove in just one place and everyone else automagically had their names removed?

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

It is worth noting that on the line $dados = parent:: dadosAnimal(); you are not assigning a method to $dados, and assigning the result of the method parent::dadosAnimal() there is it. To assign a method, one should not perform the call by hiding the parentheses (). In this way, I would be wrong in this context.

  • what I want to have as benefit is just that, to be able to change something in the parent class method and to reflect it to what is using the method, without it being necessary to change that is "inheriting".

  • In this case, you’re doing it the right way. I’m sorry if it wasn’t clear in the answer.

Browser other questions tagged

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