How can the PHP code provided be improved to adhere more closely to object-oriented programming principles?
The PHP code can be improved by encapsulating related properties and methods within a class. This will help organize the code, improve reusability, and adhere more closely to object-oriented programming principles.
class Car {
private $make;
private $model;
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
public function getMake() {
return $this->make;
}
public function getModel() {
return $this->model;
}
}
$car = new Car('Toyota', 'Camry');
echo $car->getMake(); // Output: Toyota
echo $car->getModel(); // Output: Camry
Keywords
Related Questions
- What are common issues when trying to load PHP modules in Apache and how can they be resolved?
- How can PHP developers optimize the performance of their code when working with arrays and processing a large number of URLs in a crawler application?
- How can one define a property in PHP to avoid the "Undefined property" error?