PHP OOPs Full Tutorial for Beginners

Author

Kritim Yantra

Sep 06, 2025

PHP OOPs Full Tutorial for Beginners

Let’s be honest—when most beginners start with PHP, they write everything in one file. Functions here, variables there, HTML mixed in… and soon, it becomes a big mess. Sound familiar? 😅

That’s where OOP (Object-Oriented Programming) comes to the rescue.

OOP in PHP makes your code more organized, reusable, and easier to manage. Instead of writing the same code again and again, you create blueprints (called classes) and use them to build objects. Think of it like designing a house plan once and building as many houses as you want with it.


What is OOP? (Explained Simply)

OOP is a way of writing code that revolves around objects.

  • An object is like a real-world thing (car, person, book).
  • A class is like a blueprint of that object.
  • With OOP, you can reuse and extend code easily, making your life simpler as projects grow.

👉 Imagine you’re creating a website for managing employees. Instead of writing separate functions for each employee, you create a class Employee and use it to generate multiple employees with different data. Neat, right?


Key OOP Concepts in PHP

1. Class and Object

A class is a template, and an object is an instance of that class.

<?php
class Car {
    public $brand;
    public $color;

    public function start() {
        echo "The car has started 🚗";
    }
}

// Create an object
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Red";

echo $myCar->brand; // Output: Toyota
$myCar->start();    // Output: The car has started 🚗
?>

2. Constructor

A constructor is a special method that runs automatically when you create an object.

<?php
class Car {
    public $brand;
    public $color;

    public function __construct($brand, $color) {
        $this->brand = $brand;
        $this->color = $color;
    }
}

$car1 = new Car("Honda", "Blue");
echo $car1->brand; // Honda
?>

3. Inheritance

Inheritance lets one class borrow features of another.

<?php
class Vehicle {
    public function move() {
        echo "This vehicle moves 🚙";
    }
}

class Bike extends Vehicle {
    public function ringBell() {
        echo "Ring Ring! 🔔";
    }
}

$bike = new Bike();
$bike->move();      // Inherited method
$bike->ringBell();  // Own method
?>

4. Encapsulation (Access Modifiers)

Encapsulation means protecting data by controlling access.

  • public → Accessible everywhere
  • protected → Accessible inside the class & subclasses
  • private → Accessible only inside the class
<?php
class BankAccount {
    private $balance = 0;

    public function deposit($amount) {
        $this->balance += $amount;
    }

    public function getBalance() {
        return $this->balance;
    }
}

$account = new BankAccount();
$account->deposit(500);
echo $account->getBalance(); // 500
?>

5. Polymorphism

Polymorphism allows methods to behave differently depending on the object.

<?php
class Animal {
    public function makeSound() {
        echo "Some sound";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Woof 🐶";
    }
}

class Cat extends Animal {
    public function makeSound() {
        echo "Meow 🐱";
    }
}

$dog = new Dog();
$cat = new Cat();

$dog->makeSound(); // Woof
$cat->makeSound(); // Meow
?>

6. Abstraction

Abstraction hides unnecessary details and shows only what’s important.

<?php
abstract class Shape {
    abstract public function area();
}

class Square extends Shape {
    private $side;
    public function __construct($side) {
        $this->side = $side;
    }
    public function area() {
        return $this->side * $this->side;
    }
}

$square = new Square(4);
echo $square->area(); // 16
?>

Pro Tips 💡

  • Think in terms of objects: Identify real-world entities in your project.
  • Reuse classes instead of writing duplicate functions.
  • Follow naming conventions to keep code clean.

FAQs for Beginners

Q1: Is OOP mandatory in PHP?
👉 No, but it’s highly recommended for bigger projects.

Q2: Can I mix OOP and procedural code?
👉 Yes, but for clean code, stick with one style in a project.

Q3: What’s the difference between a class and an object?
👉 A class is a blueprint, while an object is a real-world instance of that class.


Conclusion 🎯

OOP in PHP may sound overwhelming at first, but once you understand classes, objects, inheritance, and encapsulation, everything starts making sense. It’s like moving from messy notes to a well-organized notebook—you’ll thank yourself later!

👉 Now it’s your turn:
Try creating a Student Management System with classes like Student, Course, and Teacher. Play around, experiment, and see how powerful OOP can be.

Tags

Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts