Bu oop'dir:
// Define a Car class
class Car {
// Properties
public $make;
public $model;
public $year;
// Constructor method to initialize properties
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
// Method to get the full name of the car
public function getFullName() {
return $this->year . ' ' . $this->make . ' ' . $this->model;
}
}
// Instantiate the Car class
$car1 = new Car('Toyota', 'Corolla', 2020);
$car2 = new Car('Honda', 'Civic', 2018);
// Access the method to get the full name of the car
echo $car1->getFullName(); // Output: 2020 Toyota Corolla
echo "<br>";
echo $car2->getFullName(); // Output: 2018 Honda Civic
bu değildir:
// Function to create a car array
function createCar($make, $model, $year) {
return [
'make' => $make,
'model' => $model,
'year' => $year
];
}
// Function to get the full name of the car
function getFullName($car) {
return $car['year'] . ' ' . $car['make'] . ' ' . $car['model'];
}
// Create car arrays
$car1 = createCar('Toyota', 'Corolla', 2020);
$car2 = createCar('Honda', 'Civic', 2018);
// Access the function to get the full name of the car
echo getFullName($car1); // Output: 2020 Toyota Corolla
echo "<br>";
echo getFullName($car2); // Output: 2018 Honda Civic