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
- In what scenarios does it make sense to store article content in a database rather than as separate files?
- How can PHP developers effectively handle line breaks in their code without sacrificing readability or functionality?
- How can PHP developers ensure that both logged-in and non-logged-in users have access to certain parts of a website without compromising security?