Pregunta | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

16th Nov 2016, 4:13 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar
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; ?>
16th Nov 2016, 4:40 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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.
16th Nov 2016, 4:47 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
look at my pervious answer I have updated a little bit.
16th Nov 2016, 4:56 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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.
16th Nov 2016, 5:15 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
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
16th Nov 2016, 4:14 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar
0
do you get any error or something?
16th Nov 2016, 4:21 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
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
16th Nov 2016, 4:36 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar
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
16th Nov 2016, 4:45 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar
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 .
16th Nov 2016, 5:09 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar
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 ..
16th Nov 2016, 5:12 PM
Cesar Augusto Rondón
Cesar Augusto Rondón - avatar