Spex3
    

Object-Oriented Programming (OOP) in PHP


    

    

Object-Oriented Programming (OOP) in PHP
PHP supports Object-Oriented Programming (OOP), which helps in writing modular, reusable, and organized code.

1. Classes and Objects
A class is a blueprint for creating objects.
An object is an instance of a class.

✅ Example: Defining a Class & Creating an Object


<?php
class Car {
public $brand; // Property

// Method
public function setBrand($brand) {
$this->brand = $brand;
}

public function getBrand() {
return $this->brand;
}
}

// Creating an object of the Car class
$car1 = new Car();
$car1->setBrand("Toyota");
echo "Car Brand: " . $car1->getBrand();
?>


📌 Explanation:

$this->property refers to the current object.

Methods (functions inside a class) interact with class properties.

2. Properties and Methods
Properties (variables) and methods (functions) define an object’s behavior.

Access Modifiers
Modifier Description
public Accessible everywhere
private Accessible only inside the class
protected Accessible inside the class and child classes
✅ Example: Using Public and Private Properties


<?php
class User {
public $name; // Can be accessed anywhere
private $password; // Only accessible inside the class

public function setPassword($password) {
$this->password = $password;
}

public function getPassword() {
return $this->password; // Allowed inside class
}
}

$user1 = new User();
$user1->name = "Alice";
$user1->setPassword("secret123");

// Accessing public property
echo "User: " . $user1->name . "
";
// Accessing private property via a method
echo "Password: " . $user1->getPassword();
?>


📌 Why use private properties?

To prevent direct modification and protect sensitive data.

3. Inheritance
Inheritance allows a class to inherit properties and methods from another class using the extends keyword.

✅ Example: Parent & Child Classes


<?php
class Animal {
public $name;

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}

// Dog inherits from Animal
class Dog extends Animal {
public function bark() {
return "Woof! Woof!";
}
}

$dog = new Dog();
$dog->setName("Buddy");
echo $dog->getName() . " says " . $dog->bark();
?>


📌 Inheritance Benefits:

Reusability: Avoid duplicate code.

Extensibility: Easily modify or extend features.

4. Polymorphism
Polymorphism allows overriding methods in child classes.

✅ Example: Method Overriding


<?php
class Shape {
public function draw() {
return "Drawing a shape";
}
}

class Circle extends Shape {
public function draw() {
return "Drawing a Circle";
}
}

$shape = new Shape();
echo $shape->draw() . "
"; // Output: Drawing a shape

$circle = new Circle();
echo $circle->draw(); // Output: Drawing a Circle
?>


📌 Polymorphism allows:

The same method name to behave differently in different classes.

5. Encapsulation
Encapsulation protects data by restricting direct access using getters and setters.

✅ Example: Using Getters and Setters


<?php
class BankAccount {
private $balance = 0;

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

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

$account = new BankAccount();
$account->deposit(100);
echo "Balance: $" . $account->getBalance();
?>


📌 Encapsulation Benefits:

Data hiding: Prevents direct access to sensitive properties.

Controlled access: Modifies properties using methods.

6. Constructors and Destructors
A constructor (__construct()) is called automatically when an object is created.
A destructor (__destruct()) is executed when an object is destroyed.

✅ Example: Constructor & Destructor


<?php
class Person {
public $name;

public function __construct($name) {
$this->name = $name;
echo "Object created: " . $this->name . "
";
}

public function __destruct() {
echo "Object destroyed: " . $this->name . "
";
}
}

$person1 = new Person("Alice");
?>


📌 Usage:

__construct() initializes an object.

__destruct() runs when the script ends (useful for cleanup).

7. Namespaces
Namespaces help avoid class name conflicts in large applications.

✅ Example: Declaring a Namespace (Library.php)


<?php
namespace Library;

class Book {
public function info() {
return "This is a book";
}
}
?>


✅ Example: Using a Namespace (main.php)


<?php
include 'Library.php';
use Library\Book;

$book = new Book();
echo $book->info();
?>


📌 Why use namespaces?

Avoid name conflicts when using multiple classes.

Organize code in large projects.

8. Summary of OOP Concepts

OOP Concept Description Example

Classes & Objects Blueprint & instance of a class new Car();


Properties & Methods Variables & functions inside a class $this->brand;

Access Modifiers public, private, protected private $password;

Inheritance Child class inherits from parent class class Dog extends Animal

Polymorphism Overriding methods in child class public function draw()

Encapsulation Restricting direct access to properties getBalance()

Constructor Initializes an object __construct($name)

Destructor Runs when object is destroyed __destruct()

Namespace Avoids class name conflicts namespace Library;


    Date: 2025-03-28 00:00:00.000000