I have a little doubt with poo in php and it is that I have 1 class with a repaint() method (to apply a new color to a car) but I want to use that method for another vehicle class and I don't know how to put it, I have tried some things but it doesn't come out.
class vehiculo
{
private $color;
private $peso;
public function __construct($color, $peso)
{
$this->color=$color;
$this->peso=$peso;
}
}
class coche extends vehiculo
{
private $numero_puertas;
public static function repintar($color)
{
$this->color = $color;
}
}
$vehiculo1= new Vehiculo("Verde", 1500);
$vehiculo1->repintar("Negro"); //error
Thanks. Updated class name.
Do all vehicles (Cars, Trucks, Motorcycles, Bikes, Space Ships... LOL) have to be painted, or put another way, have to have a property
color
? If the answer is yes, as seems obvious, then the methodpintar()
should be in the parent class which isVehiculo
.Let's see an example where we also include a type of handling regarding the issue of the
ruedas
, discussed in comments. I clarify that it is only an example of how you could determine if you are trying to create aCoche
. You will need to implementsetRuedas()
all the necessary logic to determine the type of vehicle, preventing one fromMoto
having 50 wheels, one fromCamion
having 1 wheel and so on. I say this just to illustrate that it was the wrong way to create classes of typeCuatroRuedas
,DosRuedas
, orNRuedas
. The wheel issue is a property and should be handled/resolved in a method, not by creating classes.The example also shows how properties are passed from the child to the parent and how from the child we can call methods like
toString()
to get the piece of information corresponding to the parent (you could also define the properties likeprotected
and use them from the child).It's a very basic example where setter and getter would be missing . In fact, a more suitable method name might be maybe
setColor()
(which is the same as paint ), but respecting the usual naming convention .These are the classes:
Vehiculo
Coche
Let's look at several examples:
Departure:
With that basic example you can try to build a class
Moto
for example, or a classNaveEspacial
, and you can paint both objects from the classVehiculo
.By the way, I have used the usual naming convention for classes which is
PascalCase
.There are several concepts that are not right in the code. Static methods cannot access instance methods or attributes. Accessing attributes and instance methods requires an object initialized in that class. I will illustrate it with an example:
Now doing some tests:
So a static method cannot change the color of your Vehicle. For this it has to be an instance method.
On the other hand, class attributes
vehiculo
cannot be private if they are to be inherited by another class, they must be public or protected . See visibility concept . If you declare them private , the child class will not have access to them and the methodrepintar()
will create a new attribute$this->color
for the class objectcoche
that has nothing to do with the class objectvehiculo
.So a modified version of your code that remains functional would be:
[*] The method is needed
get_color()
to be able to return the attribute$this->color
since it is declared as protected and cannot be accessed from outside the class. If it had been declared as public , the method is not needed and the attribute can be called directly: