PHP object oriented programming: Exercises, Practice, Solution
This resource offers a total of 95 PHP Object Oriented problems for practice. It includes 19 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
The following exercises cover a range of OOP concepts in PHP and will help you practice and reinforce your understanding of object-oriented programming.
1. Rectangle Class
Write a PHP class 'Rectangle' that has properties for length and width. Implement methods to calculate the rectangle's area and perimeter.
Click me to see the sample solution
2. Circle Class
Write a PHP class called 'Circle' that has a radius property. Implement methods to calculate the circle's area and circumference.
Click me to see the sample solution
3. Abstract Shape with Subclasses
Write a PHP class called 'Shape' with an abstract method 'calculateArea()'. Create two subclasses, 'Triangle' and 'Rectangle', that implement the 'calculateArea()' method.
Click me to see the sample solution
4. Resizable Interface with Square Class
Write a PHP interface called 'Resizable' with a method 'resize()'. Implement the 'Resizable' interface in a class called 'Square' and add functionality to resize the square.
Click me to see the sample solution
5. Vehicle Class
Write a PHP class called 'Vehicle' with properties like 'brand', 'model', and 'year'. Implement a method to display the vehicle details.
Click me to see the sample solution
6. Library System Class Hierarchy
Write a PHP a class hierarchy for a library system, including classes like 'LibraryItem', 'Book', 'DVD', etc. Implement appropriate properties and methods for each class.
Click me to see the sample solution
7. Student Class
Write a PHP class called 'Student' with properties like 'name', 'age', and 'grade'. Implement a method to display student information.
Click me to see the sample solution
8. BankAccount Class
Write a PHP a class called "BankAccount" with properties like "accountNumber" and "balance". Implement methods to deposit and withdraw money from the account.
Click me to see the sample solution
9. Animal Abstract Class
Write a PHP abstract class called 'Animal' with abstract methods like 'eat()' and 'makeSound()'. Create subclasses like 'Dog', 'Cat', and 'Bird' that implement these methods.
Click me to see the sample solution
10. Person Class with __toString()
Write a PHP class called 'Person' with properties like 'name' and 'age'. Implement the '__toString()' magic method to display person information.
Click me to see the sample solution
11. Employee Extending Person
Write a class called 'Employee' that extends the 'Person' class and adds properties like 'salary' and 'position'. Implement methods to display employee details.
Click me to see the sample solution
12. Product Class with Comparable Interface
Write a class called 'Product' with properties like 'name' and 'price'. Implement the 'Comparable' interface to compare products based on their prices.
Click me to see the sample solution
13. Logger Class with Static LogCount
Write a class called 'Logger' with a static property called 'logCount' that keeps track of the number of log messages. Implement a static method to log a message.
Click me to see the sample solution
14. Math Class with Static Methods
Write a class called 'Math' with static methods like 'add()', 'subtract()', and 'multiply()'. Use these methods to perform mathematical calculations.
Click me to see the sample solution
15. File Class with Total Size Calculation
Write a PHP class called 'File' with properties like 'name' and 'size'. Implement a static method to calculate the total size of multiple files.
Click me to see the sample solution
16. Calculator Class with Private Result
Write a PHP class called 'Calculator' that has a private property called 'result'. Implement methods to perform basic arithmetic operations like addition and subtraction.
Click me to see the sample solution
17. ShoppingCart Class
Write a PHP class called 'ShoppingCart' with properties like 'items' and 'total'. Implement methods to add items to the cart and calculate the total cost.
Click me to see the sample solution
18. Logger with Singleton Design Pattern
Write a PHP class called 'Logger' that uses the singleton design pattern to ensure only one instance of the class can be created.
Click me to see the sample solution
19. Validation Class with Static Methods
Write a class called 'Validation' with static methods to validate email addresses, passwords, and other common input fields.
Click me to see the sample solution
PHP Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.