OOP organizes code into classes and objects. A class is a blueprint and an object is an instance of it. Properties store data, methods define behaviour, and access modifiers (public, private, protected) control visibility.
Classes and Objects
class Product {
// Properties
public string $name;
protected float $price;
private int $stock;
// Constructor
public function __construct(
string $name,
float $price,
int $stock = 0
) {
$this->name = $name;
$this->price = $price;
$this->stock = $stock;
}
// Method
public function getPrice(): float {
return $this->price;
}
public function isAvailable(): bool {
return $this->stock > 0;
}
}
$p = new Product("Laptop", 999.99, 10);
echo $p->name; // Laptop
echo $p->getPrice(); // 999.99
echo $p->isAvailable(); // true