PHP OOP Introduction
markdown
Object-Oriented Programming (OOP) is a method of structuring code to make it modular, reusable, and scalable. Modern PHP (especially PHP 7 and PHP 8) fully supports OOP features such as classes, objects, inheritance, polymorphism, interfaces, and traits.
This tutorial gives a beginner‑friendly introduction to PHP OOP with clear explanations and developer examples.
OOP organizes code around objects rather than functions. Objects are instances of classes, and they bundle data (properties) and behavior (methods) together.
Reusability
Cleaner and structured code
Easy to maintain and extend
Ideal for large applications
Encapsulation of logic
A class is a blueprint.
An object is an instance of a class.
<?php
class Car {
public $brand = "Tata";
public $color = "White";
public function startEngine() {
echo "Engine started";
}
}
$myCar = new Car();
echo $myCar->brand; // Output: Tata
$myCar->startEngine(); // Output: Engine started
?>
Properties = variables inside a class
Methods = functions inside a class
class Student {
public $name;
public $age;
public function introduce() {
echo "My name is " . $this->name;
}
}
$st = new Student();
$st->name = "Shubham";
$st->introduce();
__construct)A constructor runs automatically when an object is created.
class User {
public function __construct($name) {
echo "User created: $name";
}
}
$u = new User("Amit");
A class can inherit properties and methods from another class using the extends keyword.
class Animal {
public function sound() {
echo "Animal makes sound";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog barks";
}
}
d = new Dog();
$d->sound(); // From parent
$d->bark(); // Child
Access modifiers control visibility.
class BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$acc = new BankAccount();
$acc->deposit(500);
echo $acc->getBalance();
Methods behave differently depending on the object.
class Shape {
public function draw() {
echo "Drawing shape";
}
}
class Circle extends Shape {
public function draw() {
echo "Drawing circle";
}
}
$shapes = [new Shape(), new Circle()];
foreach ($shapes as $s) {
$s->draw();
}
Interfaces define method signatures but no implementation.
interface Logger {
public function log($message);
}
class FileLogger implements Logger {
public function log($message) {
echo "Logged to file: $message";
}
}
Interfaces help enforce structure in large applications.
Traits allow code reuse across multiple classes.
trait Greeting {
public function sayHello() {
echo "Hello!";
}
}
class User {
use Greeting;
}
$u = new User();
$u->sayHello();
class User {
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getInfo() {
return $this->name . " (" . $this->email . ")";
}
}
$u = new User("Shubham", "[email protected]");
echo $u->getInfo();
You learned the basics of PHP OOP:
Classes & objects
Properties & methods
Constructors
Inheritance & polymorphism
Encapsulation (private/protected/public)
Interfaces and traits
These concepts form the foundation of modern PHP frameworks like Laravel, Symfony, and CodeIgniter.
Learn PHP OOP concepts with examples. Understand classes, objects, inheritance, polymorphism, traits, and more for beginner developers.
php oop tutorial, php object oriented programming, php classes and objects, php inheritance, php traits
"PHP OOP introduction with classes, objects, and examples"