OOP: The Pattern of Creation

Master Object-Oriented Programming and build scalable architectures.

Chapter 1: Classes & Objects

Imagine you are a car manufacturer. You don't build one car at a time from scratch. You have one master blueprint, and you use it to build 1,000 real cars.

In PHP, the blueprint is called a Class. The real car that comes off the factory line is called an Object.

php
<?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

Every car has a color and a speed (Properties). Every car can also do things, like drive or honk (Methods).

Properties are like variables that live inside your class. Methods are like functions that live inside your class.

php
<?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

When a car is born, the factory immediately paints it and sets its model name. You don't wait for later!

A Constructor is a special magic method that runs automatically the moment you say new Car().

php
<?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)

Some things in a house are for guests (Public). Some things are only for family (Protected). Some things are locked in a safe (Private).

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
<?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)

A "Racecar" is a special kind of "Car". It has everything a car has, plus extra turbo buttons!

Instead of rewriting all the code, a new class can inherit everything from an existing class using the word extends.

php
<?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

A regular car might honk with a "Beep". But a Truck honks with a "HOOOOONK". The Truck overrides the default rule.

If a child class creates a method with the exact same name as the parent, the child's version takes over.

php
<?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

Some things never change. A week always has 7 days. In a class, we use "Constants" for these unbreakable rules.

We use the word const. You don't use a dollar sign for constants!

php
<?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)

What if you want to know how many cars were built in total? You can't ask one car, because it only knows about itself. You need a variable that belongs to the blueprint!

Static properties and methods belong to the Class, not the individual objects. They are shared by everyone.

php
<?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)

You can't build a generic "Animal". You can only build a Dog or a Cat. "Animal" is just an idea, a template for other animals.

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
<?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)

If a device wants to be a "Phone", it MUST have a way to dial numbers and a way to hang up. It's a strict contract.

An Interface is a list of methods that a class must have. If the class doesn't include them, PHP will crash!

php
<?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 "Bird" and a "Plane" both know how to fly. But a Bird is an animal, and a Plane is a machine. They aren't in the same family, but they share the same skill!

A Trait is a way to share code between completely different classes without using inheritance.

php
<?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

PHP has "Hidden Spells" that run automatically when something special happens. They always start with two underscores.
  • __destruct(): Runs when the object is finished and deleted.
  • __toString(): Runs if you try to echo the whole object.
  • __get(): Runs if you try to touch a property that doesn't exist.
php
<?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

If you have two blueprints named "Product", the computer will get confused. You need to put them in separate folders!

A Namespace is like a digital folder for your code. It keeps your classes organized so names don't clash.

php
<?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!

End_of_Module_01
Return to Archives