Skip to main content

PHP OOPs exercise - Basic Oops

 Here are key PHP OOP (Object-Oriented Programming) exercise questions with solutions:

  1. Basic Class and Object Exercise:

// Create a simple bank account class

class BankAccount {

    private $accountNumber;

    private $balance;


    public function __construct($accountNumber, $initialBalance = 0) {

        $this->accountNumber = $accountNumber;

        $this->balance = $initialBalance;

    }


    public function deposit($amount) {

        if ($amount > 0) {

            $this->balance += $amount;

            return true;

        }

        return false;

    }


    public function withdraw($amount) {

        if ($amount > 0 && $amount <= $this->balance) {

            $this->balance -= $amount;

            return true;

        }

        return false;

    }


    public function getBalance() {

        return $this->balance;

    }

}


// Usage

$account = new BankAccount("1234", 1000);

$account->deposit(500);    // Balance: 1500

$account->withdraw(200);   // Balance: 1300

  1. Inheritance Exercise:

class Animal {

    protected $name;

    protected $age;

    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }

    public function makeSound() {

        return "Some sound";

    }

}


class Dog extends Animal {

    private $breed;

    public function __construct($name, $age, $breed) {

        parent::__construct($name, $age);

        $this->breed = $breed;

    }


    public function makeSound() {

        return "Woof!";

    }


    public function fetch() {

        return "{$this->name} is fetching the ball!";

    }

}


// Usage

$dog = new Dog("Rex", 3, "German Shepherd");

echo $dog->makeSound();  // Outputs: Woof!

echo $dog->fetch();      // Outputs: Rex is fetching the ball!

  1. Interface Exercise:

interface Payable {

    public function calculatePay(): float;

    public function getPaymentDetails(): array;

}


class FullTimeEmployee implements Payable {

    private $monthlySalary;

    private $name;


    public function __construct($name, $salary) {

        $this->name = $name;

        $this->monthlySalary = $salary;

    }


    public function calculatePay(): float {

        return $this->monthlySalary;

    }


    public function getPaymentDetails(): array {

        return [

            'name' => $this->name,

            'type' => 'Full Time',

            'amount' => $this->calculatePay()

        ];

    }

}

class ContractEmployee implements Payable {

    private $hourlyRate;

    private $hoursWorked;

    private $name;


    public function __construct($name, $rate, $hours) {

        $this->name = $name;

        $this->hourlyRate = $rate;

        $this->hoursWorked = $hours;

    }


    public function calculatePay(): float {

        return $this->hourlyRate * $this->hoursWorked;

    }

    public function getPaymentDetails(): array {

        return [

            'name' => $this->name,

            'type' => 'Contract',

            'amount' => $this->calculatePay()

        ];

    }

}


  1. Abstract Class Exercise:

abstract class Shape {

    abstract public function calculateArea(): float;

    abstract public function calculatePerimeter(): float;

}


class Circle extends Shape {

    private $radius;


    public function __construct($radius) {

        $this->radius = $radius;

    } 

    public function calculateArea(): float {

        return pi() * pow($this->radius, 2);

    }


    public function calculatePerimeter(): float {

        return 2 * pi() * $this->radius;

    }

}


class Rectangle extends Shape {

    private $width;

    private $height;


    public function __construct($width, $height) {

        $this->width = $width;

        $this->height = $height;

    }

    public function calculateArea(): float {

        return $this->width * $this->height;

    }

    public function calculatePerimeter(): float {

        return 2 * ($this->width + $this->height);

    }

}


  1. Static Methods and Properties Exercise:

class MathHelper {

    private static $pi = 3.14159;


    public static function circleArea($radius) {

        return self::$pi * $radius * $radius;

    }


    public static function factorial($n) {

        if ($n <= 1) return 1;

        return $n * self::factorial($n - 1);

    }

}

// Usage

echo MathHelper::circleArea(5);    // Calculate circle area

echo MathHelper::factorial(5);     // Calculate factorial


  1.  PHP static properties/methods and constants.

Key points about static:

  • Static members belong to the class itself, not instances

  • Access using ClassName:: syntax or self:: within the class

  • Static methods can only access other static properties/methods

  • Can't use $this in static methods

  • Useful for utilities and counter-like functionality

class Example {

    // Static property

    public static $counter = 0;

    

    // Static method

    public static function increment() {

        self::$counter++;

        return self::$counter;

    }

    

    // Accessing static property from instance method

    public function getCount() {

        return self::$counter; // Use self:: to access static members

    }

}


// Using static members without creating an instance

Example::$counter = 5;

$count = Example::increment(); // Returns 6


// Can also access from instance, but not recommended

$obj = new Example();

$obj->getCount(); // Returns 6


Key points about constants:

  • Constants are immutable - value can't change

  • Class constants use const keyword

  • Global constants use define() or const (since PHP 5.3)

  • Constants are always public

  • Convention is to use UPPER_SNAKE_CASE

  • Use self:: to access within class

  • Good for configuration values and status codes

class Payment {

    // Class constant

    const STATUS_PENDING = 'pending';

    const STATUS_COMPLETED = 'completed';

    const TAX_RATE = 0.2;

    

    // Using constants

    public function calculateTax($amount) {

        return $amount * self::TAX_RATE;

    }

    public function isPending() {

        return $this->status === self::STATUS_PENDING;

    }

}

// Accessing constants

echo Payment::STATUS_PENDING; // 'pending'

echo Payment::TAX_RATE; // 0.2

The main difference between static properties and constants:

  • Static properties can be modified at runtime

  • Constants are fixed and can't be changed

  • Constants are slightly more memory efficient

  • Use constants for fixed values, static for changeable class-level data



// Define global constants

define('MAX_USERS', 100);

const APP_VERSION = '1.0.0'; // Since PHP 5.3


echo MAX_USERS; // 100

echo APP_VERSION; // '1.0.0'

  1. Create a class which allows the creation of objects only 3 times without external variables.

class LimitedClass {

    private static $counter = 0;

    public function __construct() {

        if (self::$counter >= 3) {

            throw new Exception("Cannot create more than 3 objects!");

        }

        self::$counter++;

    }

        // Optional: to see how many objects have been created

    public static function getCount() {

        return self::$counter;

    }

}


// Usage example:

try {

    $obj1 = new LimitedClass(); // Works

    $obj2 = new LimitedClass(); // Works

    $obj3 = new LimitedClass(); // Works

    $obj4 = new LimitedClass(); // Throws Exception

} catch (Exception $e) {

    echo $e->getMessage(); // "Cannot create more than 3 objects!"

}

// Check count

echo LimitedClass::getCount(); // Outputs: 3

  1. Encapsulation Exercise:

class Student {

    private $name;

    private $grades = [];


    public function setName($name) {

        if (strlen($name) >= 2) {

            $this->name = $name;

            return true;

        }

        return false;

    }


    public function addGrade($grade) {

        if ($grade >= 0 && $grade <= 100) {

            $this->grades[] = $grade;

            return true;

        }

        return false;

    }


    public function getAverageGrade() {

        if (empty($this->grades)) {

            return 0;

        }

        return array_sum($this->grades) / count($this->grades);

    }

}


// Usage

$student = new Student();

$student->setName("John");

$student->addGrade(85);

$student->addGrade(90);

echo $student->getAverageGrade(); // Outputs: 87.5

  1. These exercises cover:

  • Basic class creation and object instantiation

  • Inheritance

  • Interfaces

  • Abstract classes

  • Static methods and properties

  • Encapsulation

  • Method overriding

  • Constructor usage

  • Access modifiers

Key concepts demonstrated:

  1. Encapsulation (private properties with public methods)

  2. Inheritance (parent and child classes)

  3. Polymorphism (method overriding)

  4. Abstraction (interfaces and abstract classes)

  5. Static members

  6. Constructor usage

  7. Type hinting

  8. Return type declarations




Comments

Popular posts from this blog

MySQL's ACID compliance

Mysql acid compliance ACID is an acronym that stands for four key properties of database transactions: Atomicity Ensures that a transaction is treated as a single, indivisible unit of work Either all operations within a transaction are completed successfully, or none are If any part of the transaction fails, the entire transaction is rolled back to its previous state Prevents partial updates that could leave the database in an inconsistent state Consistency Guarantees that a transaction brings the database from one valid state to another valid state All data written to the database must adhere to defined rules, constraints, cascades, triggers, and other database integrity mechanisms Ensures that any transaction will not break the database's predefined rules Isolation Determines how and when changes made by one transaction become visible to other transactions Prevents interference between concurrent transactions MySQL provides different isolation levels: Read Uncommitted Read Commit...

Interview questions related to Laravel 8 updates- Laravel Interview questions

 Laravel 8 brought several updates and features to the framework. If you are preparing for an interview and expecting questions related to Laravel 8 updates, here are some potential questions: 1. What are the major features introduced in Laravel 8? Laravel Jetstream: A new application scaffolding for Laravel, providing teams with a starting point for building robust applications. Laravel Breeze: A lightweight and minimalistic front-end starter kit. Model Factory Classes: Introduction of factory classes for model factories, allowing for better organization of data seeding logic. Job Batching: A feature that allows you to easily run a batch of jobs and then perform some action when all the jobs have completed. Dynamic Blade Components: The ability to render Blade components dynamically. 2. Explain the improvements made to the Laravel job queue in version 8. Laravel 8 introduced Job Batching, which allows you to group multiple jobs into a batch and perform actions upon the completion ...