PHP Object-Oriented Programming (OOP) Basics


Object-Oriented Programming (OOP) is a programming paradigm in PHP that helps developers create reusable, modular, and organised code. OOP revolves around objects, which combine data and methods that work on that data.

Key Concepts of OOP in PHP


1. Class
A class is a blueprint for creating objects. It defines properties and methods.
example.php
  1. class Car {
  2. public $color;
  3. public $model;
  4.  
  5. public function __construct($color, $model) {
  6. $this->color = $color;
  7. $this->model = $model;
  8. }
  9.  
  10. public function drive() {
  11. return "The $this->color $this->model is driving.";
  12. }
  13. }



2. Object
An object is an instance of a class.
example.php
  1. $myCar = new Car('red', 'Tesla');
  2. echo $myCar->drive(); // Outputs: The red Tesla is driving.



3. Properties and Methods
Properties are variables belonging to a class, while methods are functions defined in the class.


4. Access Modifiers
Access modifiers control visibility:
  • public - Accessible from anywhere.
  • protected - Accessible within the class and its subclasses.
  • private - Accessible only within the class.

example.php
  1. class Example {
  2. public $publicProperty = 'Public';
  3. protected $protectedProperty = 'Protected';
  4. private $privateProperty = 'Private';
  5.  
  6. public function showProperties() {
  7. return $this->publicProperty . ', ' . $this->protectedProperty . ', ' . $this->privateProperty;
  8. }
  9. }
  10.  
  11. $obj = new Example();
  12. echo $obj->showProperties(); // Works
  13. // echo $obj->protectedProperty; // Error
  14. // echo $obj->privateProperty; // Error



5. Inheritance
A class can inherit properties and methods from another class.
example.php
  1. class Animal {
  2. public $name;
  3.  
  4. public function eat() {
  5. return "$this->name is eating.";
  6. }
  7. }
  8.  
  9. class Dog extends Animal {
  10. public function bark() {
  11. return "$this->name is barking.";
  12. }
  13. }
  14.  
  15. $dog = new Dog();
  16. $dog->name = 'Buddy';
  17. echo $dog->eat(); // Outputs: Buddy is eating.
  18. echo $dog->bark(); // Outputs: Buddy is barking.



6. Polymorphism
Polymorphism allows methods to be overridden in child classes.
example.php
  1. class Shape {
  2. public function draw() {
  3. return "Drawing a shape.";
  4. }
  5. }
  6.  
  7. class Circle extends Shape {
  8. public function draw() {
  9. return "Drawing a circle.";
  10. }
  11. }
  12.  
  13. $shape = new Circle();
  14. echo $shape->draw(); // Outputs: Drawing a circle.



7. Encapsulation
Encapsulation involves bundling data with methods that operate on that data and restricting access using access modifiers.
example.php
  1. class BankAccount {
  2. private $balance;
  3.  
  4. public function __construct($initialBalance) {
  5. $this->balance = $initialBalance;
  6. }
  7.  
  8. public function deposit($amount) {
  9. $this->balance += $amount;
  10. }
  11.  
  12. public function getBalance() {
  13. return $this->balance;
  14. }
  15. }
  16.  
  17. $account = new BankAccount(1000);
  18. $account->deposit(500);
  19. echo $account->getBalance(); // Outputs: 1500



8. Interfaces
An interface defines a contract that classes must follow.
example.php
  1. interface Logger {
  2. public function log($message);
  3. }
  4.  
  5. class FileLogger implements Logger {
  6. public function log($message) {
  7. file_put_contents('log.txt', $message);
  8. }
  9. }



9. Abstract Classes
Abstract classes cannot be instantiated and can have both abstract and regular methods.
example.php
  1. abstract class Vehicle {
  2. abstract public function move();
  3.  
  4. public function startEngine() {
  5. return "Engine started.";
  6. }
  7. }
  8.  
  9. class Bike extends Vehicle {
  10. public function move() {
  11. return "The bike is moving.";
  12. }
  13. }
  14.  
  15. $bike = new Bike();
  16. echo $bike->startEngine(); // Outputs: Engine started.
  17. echo $bike->move(); // Outputs: The bike is moving.



Conclusion


Understanding PHP OOP basics allows you to write clean, reusable, and scalable code. Practice these concepts by creating small projects like a library system or a simple e-commerce cart.
DanielXP's Avatar
Author:
Views:
7
Rating:
There are currently no comments for this tutorial, login or register to leave one.