Practice 50 PHP Object Oriented Programming Questions, TechnoVlogs

Practice 50 PHP Object Oriented Programming Questions


Q1. Write a PHP class named Car with properties make and model. Set the values for these properties and display them. 
Input: 
 Make: Toyota
 Model: Corolla  
Expected Output: 
 Make: Toyota
 Model: Corolla

Q2. Create a PHP class Person with a method greet that outputs "Hello!". Call this method. 
Input:None  
Expected Output:Hello!

Q3. Write a PHP script to define a class Circle with a method getArea that calculates the area given the radius. 
Input:Radius: 5  
Expected Output:Area: 78.54

Q4. Define a PHP class Rectangle with a constructor that accepts length and width. Calculate the area using a method getArea(). 
Input: 
 Length: 10
 Width: 5  
Expected Output:Area: 50

Q5. Write a PHP script to create a class Employee with private properties name and salary. Add public getter and setter methods for these properties. 
Input: 
 Name: Alice
 Salary: 5000  
Expected Output: 
 Name: Alice
 Salary: 5000

Q6. Write a PHP script to create a class Book with static property count. Increment the count whenever a new book is created. 
Input:Create 3 books.  
Expected Output:Total books: 3

Q7. Write a PHP class Student with a method setGrade to set the grade and getGrade to retrieve it. 
Input:Grade: A  
Expected Output:Grade: A

Q8. Write a PHP script to create a class Calculator with methods add, subtract, multiply, and divide. 
Input: 
 Add: 5, 3
 Subtract: 10, 7
 Multiply: 4, 6
 Divide: 8, 2  
Expected Output: 
 Sum: 8
 Difference: 3
 Product: 24
 Quotient: 4

Q9. Write a PHP script to define a class Animal with a method makeSound. Create a subclass Dog that overrides makeSound to return "Woof!". 
Input:Call makeSound on Dog.  
Expected Output:Woof!

Q10. Create a PHP interface Shape with a method calculateArea. Implement this interface in classes Square and Triangle. 
 Input:

Square side: 4
Triangle base: 5, height: 8 
 Expected Output:

Square area: 16
Triangle area: 20

Q11. Write a PHP class Vehicle with a method move. Use this class in another class Car via composition. 
 Input:Call move from Car.  
 Expected Output:Vehicle is moving.

Q12. Write a PHP class Person with a destructor that outputs a message when an object is destroyed. 
 Input:Create and unset a Person object.  
 Expected Output:Person object destroyed.

Q13. Create a PHP abstract class Appliance with an abstract method turnOn. Implement it in a class Fan. 
 Input:Call turnOn on Fan.  
 Expected Output:Fan is now on.

Q14. Define a PHP class User with a static method greet. Call this method without creating an object. 
 Input:None  
 Expected Output:Hello, User!

Q15. Create a PHP trait Logger with a method log. Use this trait in a class Order. 
 Input:Call log from Order.  
 Expected Output:Logging from Order.

Q16. Write a PHP script to define a final class ConstantClass that cannot be extended. 
 Input:Attempt to extend the class.  
 Expected Output:Fatal error: Cannot extend final class ConstantClass.

Q17. Write a PHP script to implement a constructor in a class Customer that initializes the properties name and email. 
 Input: 
   Name: Bob
   Email: bob@example.com 
 Expected Output: 
   Name: Bob
   Email: bob@example.com

Q18. Write a PHP script to demonstrate method chaining by defining a class Chain with methods setName and setAge. 
 Input: 
   Name: Alice
   Age: 30 
 Expected Output: 
   Name: Alice
   Age: 30

Q19. Write a PHP script to define a class BankAccount with properties balance and methods deposit and withdraw. 
 Input: 
   Deposit: 500
   Withdraw: 200 
 Expected Output:Balance: 300

Q20. Write a PHP script to create a class Product and use the __toString() method to return a string representation of the object. 
 Input:None  
 Expected Output:Product: Name - Laptop, Price - $1000

Q21. Create a PHP script to define a parent class Food and a subclass Pizza. Use the parent keyword to call the parent class constructor. 
 Input:Pizza: Margherita  
 Expected Output: 
   Food created.
   Pizza: Margherita

Q22. Write a PHP script to use the instanceof keyword to check if an object is of a specific class type. 
 Input:Object of Car.  
 Expected Output:This is a Car object.

Q23. Write a PHP class Employee with constants FULL_TIME and PART_TIME. Access these constants. 
 Input:None  
 Expected Output: 
   FULL_TIME
   PART_TIME

Q24. Write a PHP script to implement the singleton design pattern using a class Database. 
 Input:Create two instances.  
 Expected Output:Both instances are the same.

Q25. Write a PHP script to demonstrate overloading by defining __get and __set methods in a class DynamicProperties. 
 Input:Set and get a property color.  
 Expected Output:Property color: red

Q26. Write a PHP class MathOperations to demonstrate operator overloading by implementing the __call method. 
Input:Call a method add with arguments 3 and 5.  
Expected Output:Sum: 8

Q27. Create a PHP class Person and implement cloning using the __clone method to modify properties after cloning. 
Input:Clone a Person object with name John.  
Expected Output: 
 Original Name: John
 Cloned Name: John (Clone)

Q28. Write a PHP class Collection and implement the Iterator interface to iterate through an array of items. 
Input:Items: Apple, Banana, Cherry  
Expected Output: 
 Item: Apple
 Item: Banana
 Item: Cherry

Q29. Write a PHP class Logger that implements the __destruct method to save logs to a file. 
Input:Create and destroy a Logger object with message "Log saved."  
Expected Output:Log saved.

Q30. Write a PHP script to implement polymorphism by creating a base class Animal and subclasses Cat and Dog with overridden methods. 
Input:Call makeSound on Cat and Dog.  
Expected Output: 
 Cat says: Meow
 Dog says: Woof

Q31. Write a PHP class Shape with a method describe. Use anonymous classes to create specific shapes dynamically. 
Input:Create an anonymous class for Triangle.  
Expected Output:This is a Triangle.

Q32. Write a PHP script to create a class Order that uses traits Validator and Processor. Demonstrate calling methods from both traits. 
Input:Call validate and process on Order.  
Expected Output: 
 Order validated.
 Order processed.

Q33. Create a PHP class Car and define a constant WHEELS. Access it using self within the class. 
Input:None  
Expected Output:Wheels: 4

Q34. Write a PHP class Player that uses late static binding to count the number of players. 
Input:Create two Player objects.  
Expected Output:Total players: 2

Q35. Write a PHP script to define a class Customer and serialize an object of it to a string. 
Input:Name: Alice, Email: alice@example.com  
Expected Output:Serialized string of the object.

Q36. Create a PHP class Transaction that logs the transaction time using the DateTime class in the constructor. 
Input:None  
Expected Output:Transaction time: <current timestamp>

Q37. Write a PHP script to demonstrate aggregation by creating classes Author and Book. Link multiple books to an author. 
Input: 
 Author: Jane
 Books: "PHP Basics", "Advanced PHP"  
Expected Output: 
 Author: Jane
 Books: PHP Basics, Advanced PHP

Q38. Write a PHP script to demonstrate dependency injection by injecting a Database class into a User class. 
Input:Inject Database object into User.  
Expected Output:User is using the Database service.

Q39. Write a PHP class Invoice with a property amount and use magic methods __isset and __unset. 
Input:Check and unset amount.  
Expected Output: 
 Is amount set? Yes
 Amount unset.

Q40. Write a PHP script to create a base class Device and derive classes Mobile and Laptop. Use method overriding. 
Input:Call info on both classes.  
Expected Output: 
 Mobile: Portable device
 Laptop: Computing device

Q41. Write a PHP script to define a class Gallery that implements the Countable interface to count images. 
Input:Images: img1.jpg, img2.jpg, img3.jpg  
Expected Output:Total images: 3

Q42. Write a PHP script to demonstrate namespace usage by creating a class Shop\Product in a namespace Shop. 
Input:Instantiate Shop\Product.  
Expected Output:Product created.

Q43. Write a PHP script to demonstrate the factory design pattern by creating a ShapeFactory class that returns objects of Circle, Square, or Rectangle. 
Input:Create a Circle using the factory.  
Expected Output:Circle created.

Q44. Write a PHP class Membership that uses a static variable to count the number of members. 
Input:Add 2 members.  
Expected Output:Total members: 2

Q45. Write a PHP script to demonstrate an abstract class Employee with an abstract method getSalary. Implement it in subclasses Manager and Clerk. 
Input:Get salary for both.  
Expected Output: 
 Manager salary: 5000
 Clerk salary: 3000

Q46. Write a PHP script to implement object cloning and deep cloning using the __clone method. 
Input:Clone an object with nested properties.  
Expected Output:Deep clone created.

Q47. Create a PHP class Account with a private property balance. Use setter and getter methods to access it. 
Input: 
 Set balance: 1000
 Get balance  
Expected Output:Balance: 1000

Q48. Write a PHP script to demonstrate the use of anonymous functions as class properties. 
Input:Create an anonymous function that adds two numbers.  
Expected Output:Sum: 7

Q49. Write a PHP class Inventory with a private property items. Add methods to add and retrieve items. 
Input: 
 Add items: Pen, Notebook
 Get items  
Expected Output:Items: Pen, Notebook

Q50. Write a PHP script to demonstrate the observer design pattern with classes Subject and Observer. 
Input:Attach an observer to a subject and notify it.  
Expected Output:Observer notified.

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!