In the previous tutorial, you learned how PHP Classes let you group properties and methods into reusable blueprints. Now we’ll add three powerful features: constructors, PHP Inheritance, and access modifiers — tools that make your classes safer and far less repetitive.

This is Tutorial 2 of 10 in our PHP Advanced series. You will learn how constructors automatically set up new objects, how PHP Inheritance lets one class reuse another’s code, and how access modifiers control visibility.

What is a Constructor?

A constructor is a special method that runs automatically every time you create a new object. Instead of manually assigning each property one line at a time, a constructor sets everything up in a single step, right when the object is created.

In PHP, you define a constructor using the special method name __construct().

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

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

    public function showDetails() {
        echo "Name: " . $this->name . ", Age: " . $this->age;
    }
}
?>

Now creating an object becomes much shorter, since the constructor handles property assignment automatically.

<?php
$student1 = new Student("Ahmed", 22);
$student1->showDetails();
?>

Why Constructors Matter

Without a constructor, you would need three separate lines just to set up one object’s data. With a constructor, that same setup happens in a single line during instantiation, making your code cleaner and less error-prone as your classes grow.

What is PHP Inheritance?

PHP Inheritance allows one class to inherit properties and methods from another class. This prevents you from rewriting the same code across multiple related classes. The original class is called the parent class, and the new class is called the child class.

You create PHP Inheritance using the extends keyword.

<?php
class Person {
    public $name;

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

    public function greet() {
        echo "Hello, my name is " . $this->name;
    }
}

class Student extends Person {
    public $course;
}
?>

Even though Student never defines a greet() method itself, it automatically gains access to it through PHP Inheritance from the Person class.

<?php
$student1 = new Student("Sara");
$student1->greet();
?>

Overriding Parent Methods

A child class can also redefine a method it inherited, replacing the parent’s version with its own. This is called method overriding, and it’s a common pattern within PHP Inheritance.

<?php
class Student extends Person {
    public function greet() {
        echo "Hi, I'm a student named " . $this->name;
    }
}
?>

Using the parent Keyword

Sometimes you want a child class to extend the parent’s constructor rather than fully replace it. The parent keyword lets you call the parent class’s method directly from inside the child.

<?php
class Student extends Person {
    public $course;

    public function __construct($name, $course) {
        parent::__construct($name);
        $this->course = $course;
    }
}

$student1 = new Student("Bilal", "Web Development");
$student1->greet();
?>

This pattern is extremely common in real PHP Inheritance hierarchies, since it avoids duplicating the parent’s setup logic while still adding child-specific properties.

Access Modifiers: public, private, protected

Access modifiers control which parts of your code can access a property or method. PHP supports three levels: public, private, and protected.

Modifier Access Level
public Accessible from anywhere, inside or outside the class
private Accessible only within the same class
protected Accessible within the class and any child classes
<?php
class Account {
    private $balance = 1000;

    protected function showBalance() {
        echo "Balance: " . $this->balance;
    }
}
?>

Trying to access $balance directly from outside the Account class would trigger an error, since private properties stay locked to their own class, unlike public ones.

Access Modifiers with Inheritance

Protected properties strike a useful middle ground for PHP Inheritance — they stay hidden from outside code, but child classes can still access them directly.

<?php
class Person {
    protected $name;

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

class Student extends Person {
    public function showName() {
        echo "Student name: " . $this->name;
    }
}

$student1 = new Student("Ali");
$student1->showName();
?>

Here, Student successfully accesses the protected $name property inherited from Person, something a private property would never allow. For full technical details on visibility rules, refer to the official PHP visibility manual.

What’s Next?

You now understand constructors, PHP Inheritance, method overriding, the parent keyword, and access modifiers. In the next tutorial, we will explore interfaces, abstract classes, and traits — advanced tools that add even more flexibility to your object-oriented code.

If you missed the previous lesson, check out Tutorial 1: PHP Classes Guide, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named inheritance-practice.php
  • Define a parent class called Animal with a constructor that sets a name property
  • Add a method called makeSound() in Animal that echoes a generic sound message
  • Create a child class called Dog that extends Animal and overrides makeSound() with a dog-specific message
  • Use the parent keyword inside Dog’s constructor to reuse the Animal constructor, then add an extra breed property
  • Make the name property protected instead of public, and confirm Dog can still access it directly
  • Create one Dog object and call both showDetails-style output and makeSound()

Bonus Challenge: Create a class called BankAccount with a private balance property, a public constructor that sets the starting balance, and a public method called deposit() that increases the balance safely without allowing direct outside access to the property.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *