OOP: The Pattern of Creation
Master Object-Oriented Programming and build scalable architectures.
Chapter 1: Classes & Objects
In PHP, the blueprint is called a Class. The real car that comes off the factory line is called an Object.
<?php
// 1. Defining the Blueprint (Class)
class Car {
// This blueprint is currently empty.
}
// 2. Building a real car (Object) from the blueprint
$myCar = new Car();
$yourCar = new Car();
// Now we have two separate cars built from the same plan!
?>
Chapter 2: Properties & Methods
Properties are like variables that live inside your class. Methods are like functions that live inside your class.
<?php
class Car {
// Properties (What the car HAS)
public $color = "Red";
public $speed = 0;
// Methods (What the car DOES)
public function honk() {
return "Beep Beep!";
}
public function drive($newSpeed) {
$this->speed = $newSpeed; // $this refers to THIS specific car
return "Driving at " . $this->speed . " mph";
}
}
$tesla = new Car();
echo $tesla->honk(); // Prints: Beep Beep!
echo $tesla->drive(60); // Prints: Driving at 60 mph
?>
Chapter 3: The Constructor
A Constructor is a special magic method that runs automatically the moment you say new Car().
<?php
class Car {
public $model;
public $color;
// The Magic Builder
public function __construct($modelName, $paintColor) {
$this->model = $modelName;
$this->color = $paintColor;
}
}
// We pass the data right into the brackets
$myTesla = new Car("Model 3", "White");
echo "I have a " . $myTesla->color . " " . $myTesla->model;
?>
Chapter 4: Visibility (Secret Rooms)
Visibility controls who is allowed to look at or change your properties and methods.
- Public: Anyone can touch it from outside the class.
- Private: Strictly for the class itself. Nobody else can touch it!
- Protected: Only the class and its "children" can touch it.
<?php
class BankAccount {
public $userName; // Everyone can see the name
private $balance = 1000; // Only the bank can see the money!
public function showBalance() {
return $this->balance; // The class can see its own private data
}
}
$account = new BankAccount();
$account->userName = "Alex"; // OK!
// $account->balance = 5000; // ERROR! You can't touch private data.
?>
Chapter 5: Inheritance (Family Trees)
Instead of rewriting all the code, a new class can inherit everything from an existing class using the word extends.
<?php
class Car {
public $wheels = 4;
public function drive() { return "Moving..."; }
}
// Racecar gets $wheels and drive() for free!
class Racecar extends Car {
public function useNitro() { return "VROOOOM!"; }
}
$ferrari = new Racecar();
echo $ferrari->wheels; // 4 (from Parent)
echo $ferrari->useNitro(); // VROOOOM! (from Child)
?>
Chapter 6: Overriding Rules
If a child class creates a method with the exact same name as the parent, the child's version takes over.
<?php
class Car {
public function honk() { return "Beep!"; }
}
class Truck extends Car {
// This overrides the parent's honk!
public function honk() { return "HOOOOOONK!"; }
}
?>
Chapter 7: Class Constants
We use the word const. You don't use a dollar sign for constants!
<?php
class Calendar {
const DAYS_IN_WEEK = 7;
}
// We use double colon (::) to reach constants
echo Calendar::DAYS_IN_WEEK;
?>
Chapter 8: Static (Shared Mind)
Static properties and methods belong to the Class, not the individual objects. They are shared by everyone.
<?php
class Factory {
public static $carCount = 0;
public static function build() {
self::$carCount++; // self refers to the blueprint itself
return "Car built!";
}
}
Factory::build();
Factory::build();
echo Factory::$carCount; // Prints: 2
?>
Chapter 9: Abstract (Ghost Blueprint)
An Abstract Class is a "Ghost Blueprint". You aren't allowed to build an object from it directly. You can only use it to make sure other classes follow the same rules.
<?php
abstract class Animal {
// Every animal MUST make a sound, but the sound is different
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() { return "Woof!"; }
}
// $a = new Animal(); // ERROR! You can't build a ghost.
?>
Chapter 10: Interfaces (Contracts)
An Interface is a list of methods that a class must have. If the class doesn't include them, PHP will crash!
<?php
interface Phone {
public function call($number);
public function hangUp();
}
class iPhone implements Phone {
public function call($number) { return "Dialing..."; }
public function hangUp() { return "Disconnected."; }
}
?>
Chapter 11: Traits (Copy Magic)
A Trait is a way to share code between completely different classes without using inheritance.
<?php
trait Flyable {
public function fly() { return "Flying high!"; }
}
class Bird { use Flyable; }
class Plane { use Flyable; }
$crow = new Bird();
echo $crow->fly(); // Flying high!
?>
Chapter 12: Magic Methods
__destruct(): Runs when the object is finished and deleted.__toString(): Runs if you try toechothe whole object.__get(): Runs if you try to touch a property that doesn't exist.
<?php
class User {
public function __toString() {
return "This is a User object!";
}
}
$u = new User();
echo $u; // Instead of crashing, it prints our custom message!
?>
Chapter 13: Namespaces
A Namespace is like a digital folder for your code. It keeps your classes organized so names don't clash.
<?php
// This class lives in the Shop folder
namespace App\Shop;
class Product {
public $name = "Apple";
}
// To use it later:
// $p = new \App\Shop\Product();
?>
The Grand Architect
You have now learned the entire A-to-Z of Object-Oriented Programming. You are no longer just writing code—you are designing intelligent, organized systems. You are a Master Architect!