0
Pregunta
Una pregunta porque cuando heredó una clase de otra por ejemplo... class Carro { private $motor; function Carro { $this->motor=1; } } - class Gandola extends Carro { function Gandola { this-> motor=2; } } $mack= new Gandola; $fortuner=new Carro; echo $mack->motor; No entiendo porque a la hora de imprimir se visualiza el valor del motor de el camión que no no debería noestá heredando el private? Si lo coloco al revés así si funciona echo $fortuner-> motor; echo $mack->motor; Acá se visualiza
10 Answers
+ 1
use this below code
<?php
class Carro {
private $motor;
function __construct(){
$this->motor=1;
}
}
class Gandola extends Carro {
function __construct(){
$this->motor=2;
}
}
$mack= new Gandola();
$fortuner=new Carro();
echo $mack->motor;
?>
+ 1
When you try to access motor variable from Carro class, it show you an error code that you cannot access private variable. when you extend your class the derived class have all the properties of base class but variables become public. Not all variables can be public, if you define them in derived class they become public.
+ 1
look at my pervious answer I have updated a little bit.
+ 1
all variables whether it is private change to public, if you define them in derived class they become public. like you have redefine motor in derived class and it value change to 2. If you want to check create a private variable in base class that is Carro. do not define it in derived class. and call that variable from $motor object. It give you an error.
0
My question is because when I call the function that gives me the name of the engine that I value, if in fact I should not give me
0
do you get any error or something?
0
I do not understand why at the time of printing is displayed the value of the motor of the truck that should not be inheriting the private? If I put it backwards so it works
0
That code should give me an error because the private one does not let print on the main page and I am inheriting it from the class Car, that is my doubt
0
What you want to tell me is, that the private does not apply to the inheritance of Gandola. A question and a moment when the value of the engine to the class of Carro and then to the class of Gandola does not appear anything .
0
One question and because when I ask the code that the value of the engine to the class of Carro and then to the class of Gandola I do not see the value of the class gandola ..