-
Hajipur, Bihar, 844101
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects rather than functions or procedures. In PHP, OOP allows developers to model real-world entities, encapsulate data, and structure applications in a more modular and maintainable way. Understanding OOP is essential for building complex and scalable PHP applications.
OOP is a way of writing code where data and the operations on that data are grouped together as objects. Each object is an instance of a class, which acts as a blueprint. Using OOP, you can:
Represent real-world entities as objects.
Reuse code through inheritance.
Hide internal details using encapsulation.
Define common behavior through interfaces and abstract classes.
Make your code more readable, modular, and maintainable.
PHP supports all the core concepts of object-oriented programming:
Classes and Objects – A class defines the structure and behavior of an object, while an object is an instance of a class.
Properties and Methods – Properties store data, and methods define actions or behaviors.
Encapsulation – Hides the internal details of a class and exposes only what is necessary.
Inheritance – Allows a class to inherit properties and methods from another class.
Polymorphism – Lets objects of different classes be treated as objects of a common superclass.
Abstraction – Allows you to define classes with incomplete implementation that must be completed by child classes.
A class in PHP is created using the class keyword. You can define properties and methods inside a class.
Example:
class Car {
public $brand;
public $color;
public function startEngine() {
echo "The engine of " . $this->brand . " is starting.";
}
}
// Create an object
$car1 = new Car();
$car1->brand = "Toyota";
$car1->color = "Red";
// Call method
$car1->startEngine();
Here, Car is a class with two properties (brand and color) and one method (startEngine). $car1 is an object, and $this refers to the current object.
Encapsulation allows you to control access to properties and methods. PHP provides access modifiers:
public – Accessible from anywhere.
protected – Accessible only within the class and its subclasses.
private – Accessible only within the class itself.
Example:
class BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(1000);
echo $account->getBalance(); // Outputs: 1000
Here, $balance is private and cannot be accessed directly from outside the class.
Inheritance allows one class to extend another class and reuse its properties and methods.
Example:
class Vehicle {
public $brand;
public function start() {
echo "Vehicle started.";
}
}
class Bike extends Vehicle {
public function kickStart() {
echo "Bike kick-started.";
}
}
$bike = new Bike();
$bike->brand = "Honda";
$bike->start(); // Inherited method
$bike->kickStart(); // Own method
Bike inherits the start method from Vehicle but also has its own method kickStart.
Polymorphism allows objects of different classes to be treated similarly. In PHP, it’s often implemented using interfaces or method overriding.
Example:
class Animal {
public function makeSound() {
echo "Some generic sound";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Woof!";
}
}
function animalSound(Animal $animal) {
$animal->makeSound();
}
$dog = new Dog();
animalSound($dog); // Outputs: Woof!
Here, the animalSound function works with any object of type Animal or its subclasses.
Abstraction lets you define abstract classes that cannot be instantiated but provide a blueprint for child classes.
Example:
abstract class Shape {
abstract public function area();
}
class Rectangle extends Shape {
private $width;
private $height;
public function __construct($w, $h) {
$this->width = $w;
$this->height = $h;
}
public function area() {
return $this->width * $this->height;
}
}
$rect = new Rectangle(10, 5);
echo $rect->area(); // Outputs: 50
Child classes must implement the abstract methods defined in the parent class.
Code Reusability – Use inheritance and traits to reduce redundancy.
Modularity – Organize code into classes, making it easier to maintain.
Security – Encapsulation ensures controlled access to data.
Scalability – Easier to extend applications using OOP principles.
Readability – Clear separation between properties, methods, and behavior.
PHP OOPs provides a powerful way to write modular, maintainable, and scalable code. By understanding classes, objects, properties, methods, encapsulation, inheritance, polymorphism, and abstraction, you can design applications that mirror real-world scenarios. OOP not only organizes your code but also reduces redundancy, improves readability, and makes future development easier.
Create a class Book with properties title, author, and price. Create an object of this class and display all the property values.
Write a PHP program to create a class Student with a method introduce() that prints the student’s name and age. Instantiate the class and call the method.
Create a class Car with a private property speed. Add public methods setSpeed($s) and getSpeed() to set and get the speed. Demonstrate encapsulation by using these methods.
Create a class Vehicle with a method start(). Create a class Bike that extends Vehicle and adds a method kickStart(). Instantiate Bike and call both methods.
Create an abstract class Shape with an abstract method area(). Create a subclass Circle with a radius property and implement the area() method to calculate the circle’s area.
Create two classes, Dog and Cat, both having a method sound(). Write a function that takes an object of type Animal and calls the sound() method to demonstrate polymorphism.
Create a class Employee with properties name and salary. Create a method display() that prints employee details. Instantiate multiple objects and display their details.
Write a PHP program to create a class BankAccount with a private balance and methods deposit($amount) and withdraw($amount). Handle withdrawal attempts that exceed the balance.
Create a class Person with protected properties name and age. Create a subclass Student that adds a grade property and a method to display all details.
Create a class Rectangle with properties length and width. Add a method area() to calculate the area. Then create a subclass Square that inherits from Rectangle and sets both length and width to the same value.