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.
Have you tried
parent::$tipo
in daughter class?– user28595
Makes sense the term
parent
... :)– Marcos Vinicius
parent
allows the class they inherit from others to access methods and attributes marked as protected or public, works in a similar way tosuper
in java.– user28595
I can do with
$this
or for some reason it’s betterparent::
?– Marcos Vinicius
The use of
parent::
orself::
will make the access in a static way and this would generate an error of typeE_STRICT
in PHP versions from 5.4. In PHP 7 is generatedfatal error
. If the property and methods were static it would be no problem, however, it is set to non-static. Use$this->
– Daniel Omine