Object-Oriented PHP: A Beginner’s Guide with Examples (2025)

Object-Oriented PHP: A Beginner’s Guide with Examples (2025)
Share

Object-Oriented PHP: A Beginner’s Guide with Examples

Are you new to PHP and wondering how to take your coding skills to the next level? Or perhaps you’ve heard about Object-Oriented PHP (OOP) and want to understand its power? You’re in the right place! This beginner-friendly guide will walk you through the essentials of PHP object-oriented programming, complete with practical examples to help you grasp the concepts quickly.

In this PHP OOP tutorial, you’ll learn what OOP is, why it matters, and how to implement it in PHP using classes, objects, inheritance, and more. Whether you’re building a simple website or a complex web application, mastering OOP in PHP for beginners will make your code cleaner, reusable, and easier to maintain. Let’s dive in!

What is Object-Oriented Programming (OOP) in PHP?

Object-Oriented Programming is a programming paradigm that uses “objects” to represent data and methods to manipulate that data. Unlike procedural PHP, where you write a series of instructions, OOP organizes code into reusable blueprints called classes. These classes create objects—instances that hold data and perform actions.

In PHP, OOP helps you structure your code logically, making it ideal for large projects. It’s built on four key principles:

  • Encapsulation: Bundling data and methods together, restricting direct access.
  • Inheritance: Allowing one class to inherit properties and methods from another.
  • Polymorphism: Using a single interface to represent different types of objects.
  • Abstraction: Hiding complex details and showing only the essentials.

Don’t worry if these sound intimidating—we’ll break them down with examples!

Why Learn PHP OOP?

Before we jump into coding, let’s explore why PHP OOP examples are worth your time:

  1. Reusability: Write a class once and reuse it across your project.
  2. Scalability: Easily extend your code as your application grows.
  3. Maintainability: Organized code is simpler to debug and update.
  4. Real-World Use: Frameworks like Laravel and Symfony rely heavily on OOP.

Ready to learn PHP OOP? Let’s start with the basics: classes and objects.

Creating Classes and Objects in PHP

A class is like a blueprint, and an object is what you build from it. Here’s a simple example:

<?php
class Car {
    // Properties
    public $brand = "Toyota";
    public $color = "Blue";

    // Method
    public function drive() {
        return "The " . $this->color . " " . $this->brand . " is driving!";
    }
}

// Create an object
$myCar = new Car();

// Access properties and methods
echo $myCar->brand; // Outputs: Toyota
echo "<br>";
echo $myCar->drive(); // Outputs: The Blue Toyota is driving!
?>

In this PHP classes and objects example:

  • class Car defines the blueprint with properties ($brand, $color) and a method (drive()).
  • $myCar = new Car(); creates an object from the class.
  • $this refers to the current object inside the class.

Adding Constructors to Your PHP Classes

A constructor is a special method that runs when an object is created. It’s perfect for setting initial values. Here’s how:

<?php
class Person {
    public $name;
    public $age;

    // Constructor
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function introduce() {
        return "Hi, I’m " . $this->name . " and I’m " . $this->age . " years old.";
    }
}

// Create an object with constructor
$person = new Person("Alice", 25);
echo $person->introduce(); // Outputs: Hi, I’m Alice and I’m 25 years old.
?>

The __construct() method takes parameters and assigns them to the object’s properties. This is a core part of PHP programming guide basics.

Understanding Inheritance in PHP OOP

Inheritance lets a class inherit properties and methods from another class. Check this out:

<?php
class Animal {
    public $type = "Unknown";

    public function makeSound() {
        return "Some generic sound";
    }
}

class Dog extends Animal {
    public $type = "Dog";

    public function makeSound() {
        return "Woof!";
    }
}

// Create a Dog object
$dog = new Dog();
echo $dog->type; // Outputs: Dog
echo "<br>";
echo $dog->makeSound(); // Outputs: Woof!
?>

Here, Dog inherits from Animal using the extends keyword. It overrides the makeSound() method, showcasing polymorphism.

Encapsulation: Protecting Your Data

Encapsulation controls access to properties and methods using visibility keywords: public, protected, and private. Here’s an example:

<?php
class BankAccount {
    private $balance = 1000;

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

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

$account = new BankAccount();
// echo $account->balance; // Error: Cannot access private property
$account->deposit(500);
echo $account->getBalance(); // Outputs: 1500
?>

The $balance is private, so it can only be accessed or modified through methods like deposit() and getBalance().

Practical Example: Building a Mini Application

Let’s tie it all together with a small PHP OOP example—a library system:

<?php
class Book {
    private $title;
    private $author;

    public function __construct($title, $author) {
        $this->title = $title;
        $this->author = $author;
    }

    public function getDetails() {
        return $this->title . " by " . $this->author;
    }
}

class Library {
    private $books = [];

    public function addBook(Book $book) {
        $this->books[] = $book;
    }

    public function listBooks() {
        foreach ($this->books as $book) {
            echo $book->getDetails() . "<br>";
        }
    }
}

// Usage
$book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
$book2 = new Book("1984", "George Orwell");

$library = new Library();
$library->addBook($book1);
$library->addBook($book2);
$library->listBooks();
?>

This outputs:

  • The Great Gatsby by F. Scott Fitzgerald
  • 1984 by George Orwell

This example uses classes, objects, encapsulation, and method calls—a great way to practice OOP in PHP for beginners.

Where to Go From Here?

Congratulations! You’ve just scratched the surface of Object-Oriented PHP. To deepen your skills, explore advanced topics like interfaces, traits, and abstract classes. A great resource to continue learning is the official PHP documentation on OOP.

Practice is key—try building your own projects, like a blog system or a user manager, using these concepts. The more you code, the more natural PHP object-oriented programming will feel.

Conclusion

In this PHP OOP tutorial, we’ve covered the essentials: classes, objects, constructors, inheritance, and encapsulation. With these tools, you’re ready to write cleaner, more efficient PHP code. Start experimenting with PHP classes and objects today, and watch your programming skills soar!

Want more? Check out our PHP Tutorials category for additional guides and examples.

Table of Contents
Scroll to Top