๐Ÿ“ Web Development

PHP OOP: Classes, Objects and Inheritance

Aug 22, 20265 min read3 viewsBy Flow

PHP OOP โ€” object-oriented programming โ€” is a way of grouping data and the code that works on it into one unit. Everything you have written so far has been loose functions and variables. OOP gives them a home. Most people search for this as OOP with PHP, and it is the same thing โ€” the two names get used interchangeably.

You have already met the idea without noticing. An array holds values. A function does work. A class holds both: values and the functions that act on them.

What OOP actually solves

Everything you have written so far has been loose parts. A few variables here, a few functions there, all sitting in one file and all able to touch each other. For a hundred lines that is fine. For a thousand it stops being fine.

Picture a small shop page. You have $productName, $price, $stock, and functions like applyDiscount() and reduceStock(). Now add a second product. And a third. Suddenly you are juggling $productName2, or arrays of arrays, and every function needs to be told which product it is working on. Nothing stops another part of the file setting $stock to -50.

Object-oriented programming answers that with one idea: keep the data and the code that works on it in the same box, and let each box mind its own business.

One Product class describes what every product has and what it can do. Each actual product becomes its own object with its own values. applyDiscount() lives inside, so it always knows which product it belongs to. And you can lock $stock so it only changes through a method that refuses negative numbers.

That is the whole pitch. Fewer loose variables, fewer functions that need reminding what they are working on, and rules that cannot be walked around by accident.

It does take a little longer to click than loops or arrays did, because it is a way of arranging code rather than a single piece of syntax. Type the examples below rather than reading them โ€” this is a topic that lands through the fingers.

Your first class and object

A class is the blueprint. An object is a thing built from it. One blueprint, as many objects as you like.

<?php
class Car {
    public $brand = "Toyota";
}

$myCar = new Car();

echo $myCar->brand;
?>

Output:

PHP OOP output: creating a Car object with new and printing its brand property, Toyota

Three things happen here. class Car { } defines the blueprint. new Car() builds an object from it. -> reaches inside that object to get something out.

That arrow is the piece to remember. Outside a class you use $object->thing. There is no dollar sign on brand after the arrow โ€” a common early slip.

Properties and methods

The words are simple once named. A variable inside a class is a property. A function inside a class is a method.

<?php
class Car {
    public $brand = "Toyota";

    public function honk() {
        echo "Beep beep!";
    }
}

$myCar = new Car();

$myCar->honk();
?>

Output:

PHP OOP method call output printing Beep beep from the honk method

Same arrow, same idea. $myCar->honk() calls the method on that object. The brackets are there for the same reason they were in PHP functions โ€” they are what runs it.

$this: how an object talks about itself

A method often needs the objectโ€™s own data. Inside a class, $this means "the object this method was called on".

<?php
class Car {
    public $brand = "Toyota";

    public function describe() {
        echo "This car is a " . $this->brand;
    }
}

$myCar = new Car();

$myCar->describe();
?>

Output:

PHP OOP $this output: the method reads its own object's brand and prints This car is a Toyota

Remember scope from the functions lesson โ€” a function cannot see variables outside it. $this is how a method reaches its own objectโ€™s properties without breaking that rule.

The constructor: setting up an object

Hard-coding "Toyota" into the class is useless. Every car would be a Toyota. The constructor runs automatically when an object is created, so you can hand it values at that moment.

<?php
class Car {
    public $brand;

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

    public function describe() {
        echo "This car is a " . $this->brand . "<br>";
    }
}

$car1 = new Car("Honda");
$car2 = new Car("Suzuki");

$car1->describe();
$car2->describe();
?>

Output:

PHP OOP constructor output: two Car objects created with Honda and Suzuki, each keeping its own value

This is where PHP OOP starts paying for itself. One class, two objects, and each keeps its own data. $car1 knows nothing about $car2.

The name is fixed: two underscores, then construct. Spell it wrong and PHP simply never calls it โ€” no error, no warning, just a value that stays empty.

public and private

public means anything outside the class can touch it. private means only the class itself can. That word in front of every property has been doing a job all along.

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

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

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

$account = new BankAccount();

$account->deposit(500);
$account->deposit(250);

echo $account->getBalance();
?>

Output:

PHP OOP private property output showing a balance of 750 after two deposits through a public method

Now try to reach the balance directly and PHP stops you.

<?php
class BankAccount {
    private $balance = 0;
}

$account = new BankAccount();

echo $account->balance;
?>

Output:

PHP OOP fatal error when reading a private property from outside the class

That fatal error is the feature working. The balance can only change through deposit(), which means you can add a rule there โ€” reject negative amounts, log every change โ€” and be certain nothing bypasses it.

A useful habit: make properties private by default, and open them up only when you find you need to.

Inheritance: building on a class

A class can take everything another class has and then change part of it.

<?php
class Vehicle {
    public $wheels = 4;

    public function describe() {
        echo "This vehicle has " . $this->wheels . " wheels<br>";
    }
}

class Motorcycle extends Vehicle {
    public $wheels = 2;
}

$car = new Vehicle();
$bike = new Motorcycle();

$car->describe();
$bike->describe();
?>

Output:

PHP OOP inheritance output: Motorcycle extends Vehicle and reports 2 wheels instead of 4

Motorcycle never defines describe(), yet it has one โ€” it inherited it from Vehicle. It only overrode $wheels, and the inherited method picked up the new value on its own.

Use inheritance when the child genuinely is a kind of the parent. A motorcycle is a vehicle, so this fits. Reaching for it just to reuse a couple of functions usually makes a mess later.

Three mistakes with PHP OOP

Writing $this->$brand. No dollar sign after the arrow. It is $this->brand. This one catches everybody at least once.

Forgetting new. $car = Car(); tries to call a function named Car. You need new Car() to actually build an object.

Making everything public. It works, so it feels fine. Then something changes a value from the far side of the project and you have no idea what. Start private.

Common questions about PHP OOP

The questions that come up most often when classes and objects first click โ€” or refuse to.

What is the difference between a class and an object in PHP?

A class is the blueprint โ€” it describes what something has and what it can do. An object is a thing built from that blueprint with new. One class, as many objects as you like, and each object keeps its own data.

What does $this mean in PHP?

Inside a class, $this refers to the object the method was called on. It is how a method reaches its own properties, like $this->brand. Note there is no second dollar sign โ€” $this->$brand is a different thing and a very common typo.

What is the difference between public and private in PHP?

public means code outside the class can read and change it. private means only the class itself can. Making properties private lets you control every change through a method, so rules cannot be bypassed.

What is __construct used for in PHP?

It runs automatically the moment an object is created, so you can pass in the values that object needs. Two underscores, then construct โ€” spell it wrong and PHP never calls it, with no error to tell you why.

When should I use inheritance in PHP?

When the child genuinely is a kind of the parent โ€” a Motorcycle is a Vehicle. Using extends just to reuse a couple of functions usually creates a tangle you have to undo later.

Is OOP with PHP worth learning as a beginner?

Yes โ€” but after loops, functions and arrays are comfortable, not instead of them. OOP with PHP is what every framework is built on. Laravel, Symfony, WordPress internals: all objects. You cannot read real project code without it, and that is the point at which it stops feeling abstract.

What is next

PHP OOP is the shape almost all serious PHP takes. Laravel, WordPress internals, every framework you will meet โ€” all of it is objects.

You now have variables, control flow, functions, arrays and objects. The missing piece is somewhere to keep the data when the script ends. That means a database, and that is MySQL.

Need a refresher first? Go back through PHP functions or PHP arrays.

Comments

Loading comments...

Link copied to clipboard