Object Oriented Programming in PHP
Object Oriented Concepts
Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and software. These objects are instances of classes, which can contain data in the form of fields, also known as attributes; and code, in the form of procedures, also known as methods. The main principles of OOP are encapsulation, inheritance, and polymorphism.
Defining PHP Classes
In PHP, you define a class using the class
keyword. A class is a blueprint for creating objects. It defines a set of properties and methods that are common to all objects of one type.
class Car {
// Class properties and methods go here
}
Creating Objects in PHP
To create an object from a class, you use the new
keyword followed by the class name.
$myCar = new Car();
Calling Member Functions
Member functions are defined within a class and are used to perform operations on the object's properties. You call a member function on an object using the object operator ->
.
$myCar->drive(); // Calls the drive method on the $myCar object
Constructor Functions
A constructor is a special method that is automatically called when an object is created from a class. It is used to initialize the object's properties.
class Car {
public function __construct() {
// Initialize properties here
}
}
Destructor
A destructor is a special method that is automatically called when an object is destroyed or goes out of scope. It is used to perform cleanup tasks such as closing database connections or freeing up memory.
class Car {
public function __destruct() {
// Cleanup tasks go here
}
}
Inheritance
Inheritance allows a class to inherit properties and methods from another class. The class that is being inherited from is called the parent class, and the class that inherits is called the child class.
class ElectricCar extends Car {
// ElectricCar inherits properties and methods from Car
}
Function Overriding
Function overriding allows a child class to provide a different implementation of a method that is already defined in its parent class.
class ElectricCar extends Car {
public function drive() {
// Different implementation of the drive method
}
}
Public Members
Public members are accessible from anywhere, both inside and outside the class.
class Car {
public $color; // Public property
public function drive() { // Public method
// Code goes here
}
}
Private Members
Private members are only accessible within the class they are defined. They cannot be accessed from outside the class.
class Car {
private $color; // Private property
private function drive() { // Private method
// Code goes here
}
}
Protected Members
Protected members are accessible within the class they are defined and within any subclasses.
class Car {
protected $color; // Protected property
protected function drive() { // Protected method
// Code goes here
}
}
Interfaces
An interface is a contract that defines a set of methods that a class must implement. If a class implements an interface, it promises to provide an implementation for all the methods declared in the interface.
interface Drivable {
public function drive(); // Method declaration
}
class Car implements Drivable {
public function drive() {
// Implementation of the drive method
}
}
Constants
Constants are named values that cannot be changed once they are defined. They are useful for defining values that should not change throughout the execution of the program.
class Car {
const MAX_SPEED = 100; // Constant value
}
Abstract Classes
An abstract class is a class that cannot be instantiated and is meant to be extended by other classes. It can contain abstract methods, which are methods without an implementation, and concrete methods with an implementation.
abstract class Vehicle {
abstract public function move(); // Abstract method
public function stop() { // Concrete method
// Code goes here
}
}
Static Keyword
The static keyword is used to declare properties or methods as belonging to the class itself rather than an instance of the class. Static members can be accessed without creating an object of the class.
class Car {
public static $count = 0; // Static property
public static function getCount() { // Static method
return self::$count;
}
}
Final Keyword
The final keyword is used to prevent a class from being extended, or a method from being overridden.
final class Car {
// This class cannot be extended
}
Calling Parent Constructors
When creating a child class, you might want to call the parent class's constructor to initialize some of its properties. You can do this using the parent
keyword followed by the ::
operator and the constructor name.
class ElectricCar extends Car {
public function __construct() {
parent::__construct(); // Call the parent constructor
// Additional initialization goes here
}
}
Credits: Image by storyset