Build structured code - OOP in PHP brings organization, reusability, and power to your projects!
Object-Oriented Programming (OOP) lets you model real-world concepts with classes and objects, using principles like encapsulation, inheritance, and polymorphism. PHP’s OOP features make code modular and maintainable.
Define a blueprint with the class keyword.
class Car {
public $brand = "Toyota";
public function drive() {
echo "Vroom!";
}
}Instantiate a class with new.
$myCar = new Car();
echo $myCar->brand; // Outputs: Toyota
$myCar->drive(); // Outputs: Vroom!Control visibility with access modifiers.
| Modifier | Access Scope |
|---|---|
public |
Anywhere |
private |
Class only |
protected |
Class and subclasses |
Accessible everywhere.
class Dog {
public $name = "Buddy";
}
$dog = new Dog();
echo $dog->name; // Outputs: BuddyRestricted to the class.
class Cat {
private $age = 5;
public function getAge() {
return $this->age;
}
}
$cat = new Cat();
echo $cat->getAge(); // Outputs: 5
// echo $cat->age; // Error: Cannot access private propertyRun setup code with __construct().
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person = new Person("Alex");
echo $person->name; // Outputs: AlexRun cleanup with __destruct().
class FileHandler {
public function __destruct() {
echo "Cleaning up...";
}
}
$file = new FileHandler();
unset($file); // Outputs: Cleaning up...Extend a class with extends.
class Animal {
public function eat() {
echo "Yum!";
}
}
class Bird extends Animal {
public function fly() {
echo "Soaring!";
}
}
$bird = new Bird();
$bird->eat(); // Outputs: Yum!
$bird->fly(); // Outputs: Soaring!Accessible in class and subclasses.
class Vehicle {
protected $speed = 60;
}
class Bike extends Vehicle {
public function getSpeed() {
return $this->speed;
}
}
$bike = new Bike();
echo $bike->getSpeed(); // Outputs: 60Call parent constructors with parent::__construct().
class ParentClass {
public $value;
public function __construct($value) {
$this->value = $value;
}
}
class ChildClass extends ParentClass {
public function __construct($value) {
parent::__construct($value);
}
}
$child = new ChildClass(42);
echo $child->value; // Outputs: 42Use parent:: to access overridden methods.
class Base {
public function say() {
echo "Base!";
}
}
class Derived extends Base {
public function say() {
parent::say();
echo " Derived!";
}
}
$obj = new Derived();
$obj->say(); // Outputs: Base! Derived!Redefine a parent method.
class Shape {
public function area() {
return 0;
}
}
class Circle extends Shape {
public $radius;
public function area() {
return pi() * $this->radius * $this->radius;
}
}
$circle = new Circle();
$circle->radius = 5;
echo $circle->area(); // Outputs: ~78.54PHP doesn’t support true overloading, but use optional parameters or magic methods like __call().
class Calculator {
public function add($a, $b = 0, $c = 0) {
return $a + $b + $c;
}
}
$calc = new Calculator();
echo $calc->add(1); // Outputs: 1
echo $calc->add(1, 2); // Outputs: 3
echo $calc->add(1, 2, 3); // Outputs: 6With __call():
class Magic {
public function __call($name, $args) {
echo "Called $name with " . count($args) . " args!";
}
}
$magic = new Magic();
$magic->test(1, 2); // Outputs: Called test with 2 args!Load classes automatically with spl_autoload_register().
spl_autoload_register(function ($className) {
include "$className.php";
});
$car = new Car(); // Loads Car.php automaticallyCar.php:
class Car {
public function honk() {
echo "Beep!";
}
}| Concept | Key Feature | Example Use Case |
|---|---|---|
| Classes/Objects | Encapsulation | Model entities |
| Inheritance | Code reuse | Extend functionality |
| Access Modifiers | Control visibility | Protect data |
| Autoloading | Dynamic loading | Simplify includes |
- Encapsulation: Use
private/protectedfor sensitive data. - Single Responsibility: One class, one purpose.
- Naming: Use clear, PascalCase class names (e.g.,
UserProfile). - Autoloading: Prefer modern autoloaders (e.g., Composer).
class User {
private $id;
public function __construct($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
$user = new User(123);
echo $user->getId(); // Outputs: 123