PHP is a powerful programming language that supports object-oriented programming (OOP) concepts. Here are some basics of PHP OOP:
Here is an example of a basic PHP class:
class Car {
private $make;
private $model;
private $year;
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function getMake() {
return $this->make;
}
public function getModel() {
return $this->model;
}
public function getYear() {
return $this->year;
}
}
$car = new Car('Honda', 'Civic', 2021);
echo $car->getMake(); // Output: Honda
echo $car->getModel(); // Output: Civic
echo $car->getYear(); // Output: 2021
In this example, we define a Car class with three private properties (make, model, and year) and a constructor that sets these properties when a new Car object is created. We also define three public methods (getMake, getModel, and getYear) that can be used to retrieve the values of these properties. Finally, we create a new Car object and use its methods to retrieve its properties.
Copyright 2023, WebAdd, All Rights Reserved