logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

Object Oriented Programming(OOP) in PHP!


Object Oriented Programming(OOP) in PHP

PHP supports Object-Oriented Programming (OOP), allowing developers to use classes and objects to structure their code. Here's an overview of key OOP concepts in PHP:

 

1. Classes and Objects:

Creating a Class:

A class is a blueprint for creating objects. It defines properties (attributes) and methods (functions) that the objects will have.

class Car {
    // Properties
    public $make;
    public $model;

    // Methods
    public function startEngine() {
        echo "Engine started!";
    }
}

Creating Objects:

Objects are instances of a class. You create objects using the new keyword.

// Create an object of the Car class
$myCar = new Car();

// Accessing properties and calling methods
$myCar->make = "Toyota";
$myCar->model = "Camry";
$myCar->startEngine();

 

2. Constructor and Destructor:

Constructor:

A constructor is a special method that is automatically called when an object is created. It is used to initialize object properties.

class Car {
    public $make;
    public $model;

    // Constructor
    public function __construct($make, $model) {
        $this->make = $make;
        $this->model = $model;
    }
}

 

Destructor:

A destructor is a special method that is automatically called when an object is no longer referenced. It can be used to perform cleanup tasks.

class Car {
    // ...

    // Destructor
    public function __destruct() {
        echo "Car object destroyed!";
    }
}

 

3. Inheritance:

Inheritance allows a class to inherit properties and methods from another class. The extends keyword is used for inheritance.

class Truck extends Car {
    public $payload;

    public function loadCargo() {
        echo "Cargo loaded!";
    }
}

 

4. Encapsulation:

Encapsulation involves bundling the data (properties) and methods that operate on the data within a single unit (class). Properties are often declared as private or protected, and public methods are used to access or modify them.

class BankAccount {
    private $balance;

    public function __construct($initialBalance) {
        $this->balance = $initialBalance;
    }

    public function deposit($amount) {
        $this->balance += $amount;
    }

    public function getBalance() {
        return $this->balance;
    }
}

 

5. Polymorphism:

Polymorphism allows objects of different types to be treated as objects of a common base type. It is achieved through method overriding and interfaces.

interface Shape {
    public function calculateArea();
}

class Circle implements Shape {
    public $radius;

    public function calculateArea() {
        return 3.14 * $this->radius * $this->radius;
    }
}

class Rectangle implements Shape {
    public $length;
    public $width;

    public function calculateArea() {
        return $this->length * $this->width;
    }
}

 

6. Abstraction:

Abstraction involves simplifying complex systems by modeling classes based on essential properties and behaviors. Abstract classes and interfaces are used to achieve abstraction.

abstract class Shape {
    abstract public function calculateArea();
}

class Circle extends Shape {
    // ...
}

class Rectangle extends Shape {
    // ...
}

 

These are some of the fundamental concepts of Object-Oriented Programming in PHP. They provide a structured and modular approach to software development, making it easier to design, maintain, and scale applications.

 

Thank you.


Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment