Preloader

OOP Practice Questions in PHP

PHP Tutorials
codevigyaan php

OOP Practice Questions in PHP are designed to allow newcomers to practice and test their grasp of Object Oriented Programming. The PHP OOP Practice Questions are related to class, object, inheritance, encapsulation, polymorphism, interface, trait in PHP.

Beginner Level – Basic OOP Practice Questions in PHP

1. Classes and Objects

Create a class called Car with:

  • A public property brand
  • A start() method that displays “Car started”

Then create an object and call the method.

2. Constructor Practice

Create a class User with:

  • A private property name
  • A constructor that initializes the name.
  • A function getName() that returns the name

Create two objects with different names in the class, and print them.

3. Destructor Practice

Create a class Session with:

  • A constructor printing “Session started”.
  • A destructor which displays “Session ended”

Make an object and see the output.

Intermediate Level – Core OOP Practice Questions

4. Encapsulation

Create a class BankAccount:

  • Private property balance
  • Method deposit($amount)
  • Method withdraw($amount)
  • Method getBalance()

Ensure that requiring withdrawal does not cause the account to go in to a negative balance.

5. Inheritance

Assuming you will create the parent class Animal with method eat().

Create a child class Dog that:

  • Extends Animal
  • Provides a method bark()

Now call the both method you created on the child object.

6. Method Overriding

Define a class Employee with a method role() which print out “General Employee”.

Create a child class Manager that overrides role() and prints “Manager”.

7. Using parent Keyword

Modify the previous question so that:

  • The Manager class calls the parent role() method first
  • Then prints “Handles team”

Advanced Level – Abstraction & Interfaces

8. Abstract Class

Create an abstract class Shape with:

  • Abstract method area()

Create two classes:

  • Circle
  • Rectangle

Each should markout own area.

9. Interface Practice

Design an interface Payment with method pay($amount).

Create two classes:

  • CreditCardPayment
  • PaypalPayment

Both have to implement the interface.

Write a function that takes a Payment and process a payment.

10. Multiple Interfaces

Create two interfaces:

  • Logger
  • Notifier

Create a class Admin that implements both interfaces.

Real World Scenario Questions

11. Build a Simple Login System Structure

Design classes for:

  • User
  • Admin
  • Authentication

Use inheritance effectively.

12. Build a Payment Gateway System

Use:

  • Interface for payment types
  • At least two types of payment
  • A main function to process payment

13. Mini Project

Design a simple E Commerce structure with:

  • Product class
  • Cart class
  • User class
  • The payment interface

Use encapsulation, inheritance, and polymorphism.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *