Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 15

OOP in PHP – Classes, Objects & Inheritance

In this lesson: Learn OOP in PHP — classes, objects, constructors, access modifiers and inheritance — with simple examples and output.

What is OOP

Object-Oriented Programming (OOP) is a way of organising code around objects — self-contained units that hold both data (properties) and actions (methods). Instead of scattered variables and functions, you group related things into a class. This keeps big projects tidy, reusable and easier to grow.

💡 Class = the blueprint. Object = a real thing built from that blueprint.

Class and object

<?php
  class Car {
      public $color = "red";
  }

  $myCar = new Car();      // create an object
  echo $myCar->color;      // red
?>
red

class Car is the blueprint; new Car() builds a real object. You reach an object's property with the arrow ->.

Properties and methods

Properties are the object's data; methods are its actions.

<?php
  class Car {
      public $brand = "Tata";
      public function honk() {
          return "Beep from " . $this->brand;
      }
  }
  $c = new Car();
  echo $c->honk();
?>
Beep from Tata

Inside a method, $this means "this object", so $this->brand reads the current car's brand.

The constructor

__construct() runs automatically when the object is created — perfect for setup.

<?php
  class User {
      public $name;
      public function __construct($name) {
          $this->name = $name;
      }
  }
  $u = new User("Aman");
  echo $u->name;
?>
Aman

Passing "Aman" to new User("Aman") sends it to the constructor, which stores it. No separate setup call needed.

Inheritance

A child class extends a parent and reuses its code.

<?php
  class Animal {
      public function eat() { return "eating"; }
  }
  class Dog extends Animal {
      public function bark() { return "woof"; }
  }
  $d = new Dog();
  echo $d->eat();   // inherited
  echo $d->bark();  // its own
?>
eating woof

Dog automatically has eat() from Animal, plus its own bark(). This avoids rewriting shared code.

public, private, protected

KeywordReachable from
publicAnywhere
protectedThe class and its children
privateOnly inside the same class

Make internal details private so outside code cannot change them by accident. Expose only what is needed as public. This idea is called encapsulation.

Common mistakes

  • Forgetting new. A class is only a blueprint; you need new to make an object.
  • Using . instead of ->. Object members use the arrow ->, not a dot.
  • Forgetting $this inside methods. Use $this->prop to reach the object's own property.
  • Making everything public. Keep internal data private for safer, cleaner code.

Summary

  • OOP groups data and actions into classes and objects.
  • A class is a blueprint; new builds an object; reach members with ->.
  • __construct() sets up an object automatically.
  • extends gives inheritance — reuse a parent's code.
  • public/private/protected control access (encapsulation).

Next, deepen OOP with abstract classes vs interfaces, or reuse code with PHP traits.

Frequently Asked Questions

What is the difference between a class and an object?
A class is a blueprint — it describes what data and actions something has. An object is a real thing built from that blueprint. For example Car is the class; your specific red car is an object of it. One class can make many objects.
What does the constructor do?
The __construct() method runs automatically when you create an object with new. It is used to set up the object's starting values, like giving a new user their name straight away.
What is inheritance in OOP?
Inheritance lets one class reuse another class's code. A child class extends a parent and gets all its properties and methods, then can add or change its own. It avoids repeating shared code.
What is the difference between public, private and protected?
They control who can access a property or method. public is reachable from anywhere, private only inside the same class, and protected inside the class and its child classes. Use private for internal details.
What does $this mean in PHP?
$this refers to the current object — the specific one the method is running on. You use it to read or set that object's own properties, like $this->name.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPHP MySQL Connection (PDO) Your progress: 15 / 50 (30%) · Lesson 16 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free