The code you posted is not a class at all. It is just a bunch of global functions and variables with funky names. Try to avoid static properties and methods as they are not OOP. Also try to avoid public properties and getters/setters as they are just procedural programming in disguise.
A skeleton of a Car
class could look like this:
class Car {
private $wheel_count;
private $name;
private $color;
public function __construct($wheel_count, $name, $color)
{
$this->wheel_count = $wheel_count;
$this->name = $name;
$this->color = $color;
}
public function paint($new_color)
{
$this->color = $new_color;
}
public function describe()
{
return sprintf('%d wheels, %s %s', $this->wheel_count, $this->color, $this->name);
}
}
The object properties are private. Encapsulation is one of the key concepts of OOP. They are set in the constructor. The role of the constructor is to initialize the object, to make it ready to work. Make sure the constructor initializes all object's properties and it doesn't do anything else (no work in the constructor.)
Create methods in the class for the actions that make sense for your class. For example, the number of wheels of a car never changes, there is no point in writing a method to set a different number of wheels. But the color of the car sometimes changes and the paint()
method handles that.
Avoid writing "getters" (i.e. methods that do nothing than returning the value of a property). They are the sign that some code that belongs to the class is written somewhere outside the class (and many times it is duplicated here and there and everywhere, with minor or no changes). Whenever is possible and appropriate, write a method that uses the object's properties to generate an useful value (as opposed to writing getters to let the useful value be computed somewhere else).
This is how the Car
class described below is used:
$car1 = new Car(4, 'Mazda', 'red');
$car2 = new Car(8, 'big truck', 'blue');
echo($car1->describe());
$car1->paint('green');
echo($car1->describe());