🟢 Beginner Level
1. Class & Object
class Car {
public $brand;
public $model; public function showDetails() {
echo "Brand: $this->brand, Model: $this->model";
}
}$car1 = new Car();
$car1->brand = "Toyota";
$car1->model = "Fortuner";
$car1->showDetails();
2. Constructor
class Student {
public $name;
public $age; public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
} public function show() {
echo "$this->name is $this->age years old";
}
}$s = new Student("Aditya", 22);
$s->show();
3. Calculator
class Calculator {
public function add($a, $b) {
return $a + $b;
}
public function subtract($a, $b) {
return $a - $b;
}
public function multiply($a, $b) {
return $a * $b;
}
public function divide($a, $b) {
return $b != 0 ? $a / $b : "Cannot divide by zero";
}
}$c = new Calculator();
echo $c->add(5, 3);
🟡 Intermediate Level
4. Inheritance
class Animal {
public function makeSound() {
echo "Animal sound";
}
}class Dog extends Animal {
public function makeSound() {
echo "Bark";
}
}class Cat extends Animal {
public function makeSound() {
echo "Meow";
}
}$d = new Dog();
$d->makeSound();
5. Encapsulation
class BankAccount {
private $balance = 0; public function deposit($amount) {
$this->balance += $amount;
} public function withdraw($amount) {
if ($amount <= $this->balance) {
$this->balance -= $amount;
} else {
echo "Insufficient balance";
}
} public function getBalance() {
return $this->balance;
}
}$acc = new BankAccount();
$acc->deposit(1000);
$acc->withdraw(300);
echo $acc->getBalance();
6. Static
class Counter {
public static $count = 0; public static function increment() {
self::$count++;
}
}Counter::increment();
Counter::increment();
echo Counter::$count;
7. Constants
class MathConstants {
const PI = 3.14; public function circleArea($r) {
return self::PI * $r * $r;
}
}$m = new MathConstants();
echo $m->circleArea(5);
🔵 Advanced Level
8. Abstract Class
abstract class Shape {
abstract public function area();
}class Circle extends Shape {
private $r; public function __construct($r) {
$this->r = $r;
} public function area() {
return 3.14 * $this->r * $this->r;
}
}class Rectangle extends Shape {
private $w, $h; public function __construct($w, $h) {
$this->w = $w;
$this->h = $h;
} public function area() {
return $this->w * $this->h;
}
}
9. Interface
interface Payment {
public function pay($amount);
}class CreditCard implements Payment {
public function pay($amount) {
echo "Paid $amount using Credit Card";
}
}class PayPal implements Payment {
public function pay($amount) {
echo "Paid $amount using PayPal";
}
}
10. Polymorphism
class Circle {
public function draw() {
echo "Drawing Circle";
}
}class Square {
public function draw() {
echo "Drawing Square";
}
}function render($shape) {
$shape->draw();
}render(new Circle());
render(new Square());
11. Magic Methods
class User {
public $name; public function __construct($name) {
$this->name = $name;
echo "User Created<br>";
} public function __destruct() {
echo "User Destroyed";
} public function __toString() {
return "User: " . $this->name;
}
}$u = new User("Aditya");
echo $u;
12. File Handling
class FileManager {
public function write($file, $data) {
file_put_contents($file, $data);
} public function read($file) {
return file_get_contents($file);
}
}$f = new FileManager();
$f->write("test.txt", "Hello World");
echo $f->read("test.txt");
🔴 Real-World
13. Simple Login System (Basic)
class User {
private $username;
private $password; public function register($u, $p) {
$this->username = $u;
$this->password = password_hash($p, PASSWORD_DEFAULT);
} public function login($u, $p) {
if ($this->username === $u && password_verify($p, $this->password)) {
return "Login Success";
}
return "Login Failed";
}
}$user = new User();
$user->register("admin", "1234");
echo $user->login("admin", "1234");
14. Shopping Cart
class Product {
public $name, $price; public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}class Cart {
private $items = []; public function add(Product $p) {
$this->items[] = $p;
} public function total() {
$sum = 0;
foreach ($this->items as $item) {
$sum += $item->price;
}
return $sum;
}
}$cart = new Cart();
$cart->add(new Product("Phone", 10000));
$cart->add(new Product("Laptop", 50000));
echo $cart->total();
15. Blog System (Basic Structure)
class Post {
public $title; public function __construct($title) {
$this->title = $title;
}
}class Comment {
public $text; public function __construct($text) {
$this->text = $text;
}
}class User {
public $name; public function __construct($name) {
$this->name = $name;
}
}