How to send attributes to extended classes?

Asked

Viewed 431 times

1

class Veiculos
{
    protected $tipo = "carro";
}

Below I am trying to identify which type of vehicle the class Veiculos assigned. I created a function tipo_veiculo that should show the user what type of vehicle he chose and ask him to choose a color in the next step.

class Veiculo extends Veiculos
{
    public function tipo_veiculo(){
        if($tipo == "carro"){
            echo "Você selecionou carro. Escolha a cor.";
        } elseif($tipo == "moto") {
            echo "Você selecionou moto. Escolha a cor.";
        } else {
            echo "Você não selecionou nenhum carro.";
        }
    }
}

How to send the attribute $tipo so that the class Veículo can use it? I tried to write something using the concept of inheritance so I can try to get an answer.

  • Have you tried parent::$tipo in daughter class?

  • Makes sense the term parent ... :)

  • 1

    parent allows the class they inherit from others to access methods and attributes marked as protected or public, works in a similar way to super in java.

  • I can do with $this or for some reason it’s better parent::?

  • The use of parent:: or self:: will make the access in a static way and this would generate an error of type E_STRICT in PHP versions from 5.4. In PHP 7 is generated fatal error. If the property and methods were static it would be no problem, however, it is set to non-static. Use $this->

1 answer

6


Understanding inheritance

It is difficult to answer this question because it has a conceptual error. And if it does not understand the concepts correctly it will not encode correctly.

When you inherit a class, the class that gave rise to that is part of it, you put it all together. In other words, the child class is all that the mother class is anything else. They are not two separate things that can communicate (send), it is one thing only.

It’s weird enough a class Veiculo inherit from a class called Veiculos. I’ve already mentioned another question that there has to be a reason to use a class and more to use inheritance. Almost nothing needs inheritance, inheritance should be the exception, even the most OOP fanatics know it nowadays.

One of the most important things in coding is to give good names to things. A thing that calls Veiculos must have several vehicles inside it? Does this class have? It does not seem. And if it is a thing that has several vehicles, why would it be part of a single vehicle? Are you going to put several vehicles inside a vehicle? It doesn’t make sense.

If this class is a part of a vehicle, then it should have another name. And if it is only a part there should be composition and non-inheritance (see also here). I just can’t imagine how the inheritance would be appropriate in this case.

The specific problem

Having said all that is important for those who want to learn with this question, just access the variable in the right way. After all $tipo is a local variable (of the function), already $this->tipo is the instance variable of the class. The error is different from the one described in the question.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • So whatever parent or $this->, since in the class you are inheriting there is no method/attribute being invoked?

  • It’s a great explanation .... I was able to understand that inheritances are true exceptions and should be used with caution, I also understood that all the content of the class that passes into the subclass has to be related to the same.

  • 1

    @Diegofelipe No, only this->can be used. The property $tipos becomes part of the daughter class. The parent is rarely necessary. Nor can it be used for instance variables. It is used when there is conflict between child-mother method implementations. If there are two implementations of the same name in both classes, obviously the preference will be for the daughter’s method, that’s where the parent, is the only way to access the mother’s implementation (obviously if there is a reimplementation of the method). It is also used for static members.

  • @Marcosvinicius is strange $tipo being in the mother class. Maybe this is an artificial example. Then it doesn’t help to learn. OOP is interesting to learn from examples that at least simulate real problems. What is right or not in OOP depends a lot on what the real concept is like. If the concept is invented, then everything can be wrong. If the problems could be modeled as we want, OOP would not be needed for anything. It helps to model real problems. If Carro inherit from Veiculo would make more sense. But obviously depending on what you have within the classes, it’s not enough that the name is appropriate.

  • Artificial example ... I am studying to learn the concepts and I am improving every day. Thank you.

Browser other questions tagged

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