codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

43 / 56 topics
43Object-Oriented Programming in PHP44Classes and Objects45Inheritance in PHP46Polymorphism in PHP47Interfaces in PHP48Traits in PHP
Tutorials/PHP/Object-Oriented Programming in PHP
🐘PHP

Object-Oriented Programming in PHP

Updated 2026-05-15
10 min read

Object-Oriented Programming in PHP

Introduction

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code to manipulate that data. OOP is widely used in modern software development due to its ability to model real-world entities effectively, promote code reusability, and simplify complex systems.

In PHP, OOP concepts are implemented through classes and objects. This tutorial will introduce you to the basics of object-oriented programming in PHP, including how to define classes, create objects, and use inheritance and polymorphism.

Concepts

Classes and Objects

A class is a blueprint for creating objects. It defines properties (attributes) and methods (functions) that the created objects can use. An object is an instance of a class.

Defining a Class

To define a class in PHP, you use the class keyword followed by the class name. Here's a simple example:

php
1class Car {
2 // Properties
3 public $color;
4 public $model;
5
6 // Constructor method
7 public function __construct($color, $model) {
8 $this->color = $color;
9 $this->model = $model;
10 }
11
12 // Method to display car information
13 public function displayInfo() {
14 echo "Car color: " . $this->color . "<br>";
15 echo "Car model: " . $this->model . "<br>";
16 }
17}

Creating an Object

To create an object from a class, you use the new keyword followed by the class name and any required parameters for the constructor:

php
1$myCar = new Car("red", "Toyota Camry");
2$myCar->displayInfo();
Output
Car color: red
Car model: Toyota Camry

Encapsulation

Encapsulation is the concept of bundling the data (properties) and methods that operate on the data into a single unit or class. It also restricts direct access to some of an object's components, which can prevent the accidental modification of data.

In PHP, you can use access modifiers (public, protected, private) to control the visibility of properties and methods:

  • public: Accessible from anywhere.
  • protected: Accessible within the class and by inheriting classes.
  • private: Accessible only within the class.
php
1class Car {
2 private $color;
3 protected $model;
4
5 public function __construct($color, $model) {
6 $this->color = $color;
7 $this->model = $model;
8 }
9
10 public function getColor() {
11 return $this->color;
12 }
13
14 protected function getModel() {
15 return $this->model;
16 }
17}

Inheritance

Inheritance is a mechanism where you can create a new class based on an existing class. The new class, known as the subclass or derived class, inherits properties and methods from the existing class, known as the superclass or base class.

php
1class Vehicle {
2 public $type;
3
4 public function __construct($type) {
5 $this->type = $type;
6 }
7
8 public function displayType() {
9 echo "Vehicle type: " . $this->type . "<br>";
10 }
11}
12
13class Car extends Vehicle {
14 private $color;
15
16 public function __construct($type, $color) {
17 parent::__construct($type);
18 $this->color = $color;
19 }
20
21 public function displayColor() {
22 echo "Car color: " . $this->color . "<br>";
23 }
24}

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. This is often achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

php
1class Animal {
2 public function makeSound() {
3 echo "Some generic animal sound<br>";
4 }
5}
6
7class Dog extends Animal {
8 public function makeSound() {
9 echo "Woof!<br>";
10 }
11}
12
13class Cat extends Animal {
14 public function makeSound() {
15 echo "Meow!<br>";
16 }
17}

Examples

Let's put these concepts together in a complete example:

php
1class Vehicle {
2 public $type;
3
4 public function __construct($type) {
5 $this->type = $type;
6 }
7
8 public function displayType() {
9 echo "Vehicle type: " . $this->type . "<br>";
10 }
11}
12
13class Car extends Vehicle {
14 private $color;
15
16 public function __construct($type, $color) {
17 parent::__construct($type);
18 $this->color = $color;
19 }
20
21 public function displayColor() {
22 echo "Car color: " . $this->color . "<br>";
23 }
24}
25
26$myVehicle = new Vehicle("Sedan");
27$myVehicle->displayType();
28
29$myCar = new Car("SUV", "Blue");
30$myCar->displayType();
31$myCar->displayColor();
Output
Vehicle type: Sedan
Vehicle type: SUV
Car color: Blue

What's Next?

In the next section, we will dive deeper into classes and objects, exploring more advanced OOP concepts such as interfaces, abstract classes, and static methods.

Stay tuned for more tutorials on PHP and object-oriented programming!


PreviousCross-Site Scripting (XSS)Next Classes and Objects

Recommended Gear

Cross-Site Scripting (XSS)Classes and Objects