Object-Oriented Programming (OOP) in PHP allows developers to build scalable, reusable, and maintainable applications. This guide explores advanced OOP concepts and demonstrates their application with practical examples.
Key Concepts in Advanced OOP
- Inheritance
- Polymorphism
- Abstraction
- Interfaces
- Traits
- Namespaces
- Dependency Injection
- Design Patterns
1. Inheritance
Inheritance allows a class to inherit properties and methods from another class.
Example:
<?php
class Vehicle {
protected $make;
public function __construct($make) {
$this->make = $make;
}
public function getMake() {
return $this->make;
}
}
class Car extends Vehicle {
private $model;
public function __construct($make, $model) {
parent::__construct($make);
$this->model = $model;
}
public function getModel() {
return $this->model;
}
}
$car = new Car("Toyota", "Corolla");
echo $car->getMake() . " " . $car->getModel();
2. Polymorphism
Polymorphism allows methods to behave differently based on the object instance.
Example:
<?php
interface Shape {
public function area();
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return pi() * pow($this->radius, 2);
}
}
class Rectangle implements Shape {
private $width, $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function area() {
return $this->width * $this->height;
}
}
$shapes = [new Circle(5), new Rectangle(4, 6)];
foreach ($shapes as $shape) {
echo "Area: " . $shape->area() . "\n";
}
3. Abstraction
Abstract classes define a blueprint for derived classes.
Example:
<?php
abstract class Animal {
abstract public function sound();
public function sleep() {
echo "Sleeping...\n";
}
}
class Dog extends Animal {
public function sound() {
return "Woof!";
}
}
$dog = new Dog();
echo $dog->sound();
$dog->sleep();
4. Interfaces
Interfaces define a contract that implementing classes must follow.
Example:
<?php
interface Logger {
public function log($message);
}
class FileLogger implements Logger {
public function log($message) {
echo "Logging to a file: $message\n";
}
}
class DatabaseLogger implements Logger {
public function log($message) {
echo "Logging to a database: $message\n";
}
}
$logger = new FileLogger();
$logger->log("This is a log message.");
5. Traits
Traits allow code reuse in single inheritance structures.
Example:
<?php
trait LoggerTrait {
public function log($message) {
echo "Log: $message\n";
}
}
class User {
use LoggerTrait;
public function createUser($name) {
$this->log("User $name created.");
}
}
$user = new User();
$user->createUser("John");
6. Namespaces
Namespaces help avoid class name collisions.
Example:
<?php
namespace App\Controllers;
class UserController {
public function index() {
echo "User list";
}
}
namespace App\Models;
class User {
public function getName() {
return "John Doe";
}
}
// Accessing namespaces
$userController = new \App\Controllers\UserController();
$userController->index();
7. Dependency Injection
Dependency Injection improves code testability and maintainability.
Example:
<?php
class Service {
public function doSomething() {
return "Service doing something.";
}
}
class Client {
private $service;
public function __construct(Service $service) {
$this->service = $service;
}
public function execute() {
return $this->service->doSomething();
}
}
$service = new Service();
$client = new Client($service);
echo $client->execute();
8. Design Patterns
Singleton Pattern
<?php
class Singleton {
private static $instance;
private function __construct() {}
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
$instance = Singleton::getInstance();
Factory Pattern
<?php
class Product {
public function __construct($name) {
$this->name = $name;
}
}
class ProductFactory {
public static function create($name) {
return new Product($name);
}
}
$product = ProductFactory::create("Book");
Advanced OOP in PHP allows you to write clean, efficient, and scalable code. By mastering these concepts, you can build better applications and follow best practices in software development. Hope this is helpful, and I apologize if there are any inaccuracies in the information provided.
Post a Comment for "Advanced Object-Oriented Programming (OOP) in PHP"