Object-Oriented Programming (OOP) organizes code into classes and objects. A class is a blueprint; an object is an instance of that class.
Use class to define a class, new to create an object, and __construct() as the constructor. Access members with -> and use $this to refer to the current object.
<?php
class Car {
// Properties
public string $make;
public string $model;
private int $speed = 0;
// Constructor
public function __construct(string $make, string $model) {
$this->make = $make;
$this->model = $model;
}
// Methods
public function accelerate(int $amount): void {
$this->speed += $amount;
}
public function getSpeed(): int {
return $this->speed;
}
// Destructor
public function __destruct() {
echo "{$this->make} {$this->model} destroyed\n";
}
// __toString magic method
public function __toString(): string {
return "{$this->make} {$this->model} @ {$this->speed} km/h";
}
}
$car = new Car("Toyota", "Corolla");
$car->accelerate(60);
echo $car->getSpeed(); // 60
echo $car; // Toyota Corolla @ 60 km/h
?>
public - accessible everywhere. protected - accessible within the class and subclasses. private - accessible only within the class.
<?php
class BankAccount {
public string $owner;
protected float $balance;
private string $pin;
public function __construct(string $owner, float $balance, string $pin) {
$this->owner = $owner;
$this->balance = $balance;
$this->pin = $pin;
}
public function deposit(float $amount): void {
$this->balance += $amount;
}
public function getBalance(): float {
return $this->balance;
}
private function validatePin(string $pin): bool {
return $this->pin === $pin;
}
public function withdraw(float $amount, string $pin): bool {
if ($this->validatePin($pin) && $this->balance >= $amount) {
$this->balance -= $amount;
return true;
}
return false;
}
}
$acc = new BankAccount("Alice", 1000.0, "1234");
$acc->deposit(500);
echo $acc->getBalance(); // 1500
echo $acc->withdraw(200, "1234") ? "Success" : "Failed"; // Success
?>
Static members belong to the class itself, not to any instance. Access them with :: (scope resolution operator).
<?php
class Counter {
private static int $count = 0;
const VERSION = "1.0";
public function __construct() {
self::$count++;
}
public static function getCount(): int {
return self::$count;
}
public static function reset(): void {
self::$count = 0;
}
}
$a = new Counter();
$b = new Counter();
$c = new Counter();
echo Counter::getCount(); // 3
echo Counter::VERSION; // 1.0
Counter::reset();
echo Counter::getCount(); // 0
?>
class ClassName {}.
__construct() is the constructor - runs automatically when an object is created with new.
public (anywhere), protected (class + subclasses), private (class only).
$this refers to the current object instance inside a class method.
ClassName::method().
__toString(), __get(), __set(), __call(), __destruct().
public function __construct(public string $name, private int $age) {}
class User { public $password; }
class User { private string $password; }
$obj->method = function(){};
Define methods inside the class body
class God { /* everything */ }
Split into User, Auth, Profile, etc.
Abstract class: can have implemented methods, properties, and constructor. A class can extend only one abstract class. Interface: only method signatures (no implementation). A class can implement multiple interfaces. Use interfaces for contracts, abstract classes for shared base behavior.
Traits are reusable code blocks that can be included in multiple classes using use TraitName. They solve the problem of single inheritance by allowing code reuse across unrelated classes. Traits can have methods and properties.
Late static binding uses static:: instead of self::. self:: always refers to the class where the method is defined. static:: refers to the class that was called at runtime - useful in inheritance chains.
Explore 500+ free tutorials across 20+ languages and frameworks.