Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.”
- Objects are instances of classes and encapsulate data (properties) and behavior (methods) that operate on that data.
- OOP enables the design and organization of code in a way that mimics real-world entities, promoting reusability, scalability, and maintainability.
OOP Terminologies:
1.) Class
A class is a blueprint or template for creating objects.
- It defines the properties (attributes) and methods (functions) that the objects created from the class will have.
class Car {
public $make;
public $model;
public function drive() {
echo "The car is driving.";
}
}
2.) Object
An object is an instance of a class. It represents a specific realization of a class and can access the properties and methods defined in the class.
$car1 = new Car(); // Create an object of the Car class
$car1->make = "Toyota";
$car1->model = "Corolla";
echo $car1->make; // Output: Toyota
3.) Constructor
A constructor is a special method in a class that is automatically called when an object of the class is created. It is typically used to initialize properties of the object.
class Car {
public $make;
public $model;
// Constructor
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
public function displayCar() {
echo "Car: $this->make $this->model";
}
}
$car1 = new Car("Honda", "Civic");
$car1->displayCar(); // Output: Car: Honda Civic
4.) Destructor
A destructor is a special method that is automatically called when an object is no longer in use or goes out of scope.
- It is commonly used for cleanup activities like closing a database connection.
class Car {
public function __construct() {
echo "Car created.<br>";
}
// Destructor
public function __destruct() {
echo "Car destroyed.<br>";
}
}
$car1 = new Car();
unset($car1); // Explicitly destroying the object
5.) Access Modifiers
Access modifiers control the visibility and accessibility of properties and methods in a class.
PHP provides three access modifiers:
- Public: Accessible from anywhere.
- Private: Accessible only within the class where it is declared.
- Protected: Accessible within the class and by derived (child) classes.
class Car {
public $make = "Toyota"; // Accessible from anywhere
private $model = "Corolla"; // Accessible only within this class
protected $year = 2022; // Accessible in this class and child classes
public function getModel() {
return $this->model; // Access private property using a public method
}
}
$car1 = new Car();
echo $car1->make; // Output: Toyota
echo $car1->getModel(); // Output: Corolla
6.) Inheritance
Inheritance is an object-oriented programming (OOP) concept where one class (child or derived class) inherits the properties and methods of another class (parent or base class).
- This promotes code reusability and allows extending or modifying the behavior of the parent class.
class ParentClass {
public $name = "Parent";
public function greet() {
return "Hello from " . $this->name;
}
}
class ChildClass extends ParentClass {
public $name = "Child";
}
$child = new ChildClass();
echo $child->greet(); // Outputs: Hello from Child
7.) Constants
Constants are like variables except that once they are defined, they cannot be changed or undefined.
- They do not use the $ sign and are defined using the define() function or the const keyword (since PHP 7).
Examples:
<?php
// Using define()
define("SITE_NAME", "My Website");
echo SITE_NAME; // Outputs: My Website
// Using const (since PHP 7)
const DB_HOST = "localhost";
echo DB_HOST; // Outputs: localhost
?>
8.) Abstract Classes
An abstract class is a class that cannot be instantiated and is meant to be extended by other classes.
- It may include both complete and abstract methods (methods without implementation).
- Child classes must provide implementations for abstract methods.
abstract class Animal {
abstract public function makeSound(); // Abstract method
public function eat() {
echo "This animal eats food.";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Bark!";
}
}
$dog = new Dog();
$dog->makeSound(); // Outputs: Bark!
9.) Interfaces
An interface defines a contract that implementing classes must follow. Unlike abstract classes, interfaces can only contain method declarations (without implementations) and cannot have properties.
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow!";
}
}
$cat = new Cat();
$cat->makeSound(); // Outputs: Meow!
10.) Static Methods and Properties
Static methods and properties belong to the class itself rather than any instance of the class. They can be accessed without creating an object of the class.
class ClassName {
public static $staticProperty = "Static Property";
public static function staticMethod() {
echo "This is a static method.";
}
}
echo ClassName::$staticProperty; // Accessing static property
ClassName::staticMethod(); // Accessing static method
11.) Namespace
Namespaces are a way to encapsulate and organize classes, functions, and constants to avoid name collisions in larger applications or when integrating third-party libraries.
namespace NamespaceOne;
class MyClass {
public function greet() {
echo "Hello from NamespaceOne!";
}
}
namespace NamespaceTwo;
class MyClass {
public function greet() {
echo "Hello from NamespaceTwo!";
}
}
$obj1 = new \NamespaceOne\MyClass();
$obj1->greet(); // Outputs: Hello from NamespaceOne!
$obj2 = new \NamespaceTwo\MyClass();
$obj2->greet(); // Outputs: Hello from NamespaceTwo!