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 some recommended resources or tutorials for beginners to improve their understanding of web programming and PHP basics?
- What are the potential pitfalls of using MySQL queries directly in PHP code without proper error handling?
- How can the "open_basedir restriction" error be resolved in PHP scripts?