This repository contains simple scenario-based tasks to practice Object-Oriented Programming (OOP) in PHP.
Understand the basics of classes, objects, properties, and methods.
Create a Product
class in PHP with the following properties:
name
price
description
Implement methods to set and get these properties, and a method displayProductDetails()
to output the product details in a readable format.
- The class should have private properties.
- Use constructor overloading to initialize the object properties.
- Implement getter and setter methods for each property.
- Create an instance of the
Product
class and use its methods to demonstrate functionality.
Practice using classes, objects, and basic object interactions.
Create a ShoppingCart
class that can hold multiple Product
objects. The cart should support adding, removing, and calculating the total price of products.
- The
ShoppingCart
class should have a private property to store an array ofProduct
objects. - Implement the following methods:
addProduct(Product $product)
- Adds a product to the cart.removeProduct(Product $product)
- Removes a product from the cart.calculateTotalPrice()
- Calculates and returns the total price of all products in the cart.
- Demonstrate functionality by adding and removing products and calculating the total price.
Understand inheritance and polymorphism.
Create a library management system with different types of materials: Book, Magazine, and DVD. Each material type should inherit from a base class LibraryMaterial
and override its methods where necessary.
- The
LibraryMaterial
class should have properties like:title
author
ISBN
publicationYear
- Create subclasses
Book
,Magazine
, andDVD
, each with specific properties like:pageCount
(for books)duration
(for DVDs)
- Implement methods in the base class and override them in the subclasses to demonstrate polymorphism.
- Create a method
displayMaterialDetails()
that outputs the details of each material.
Learn about encapsulation and data validation.
Create a Contact
class that represents a person's contact information (e.g., name, email, phone number). Include validation for email format and phone number length.
- Use private properties for
name
,email
, andphone number
. - Implement setter methods that include validation:
- Ensure the email contains an "@" symbol.
- Validate the phone number length.
- Implement getter methods to retrieve contact information.
- Create a test script that demonstrates setting and getting properties with validation.
- Ensure you have PHP installed on your system.
- Clone this repository and navigate to the respective task directories.
- Run the PHP files using the command:
php filename.php
- Observe the output to verify the implemented functionalities.
These tasks help in understanding fundamental OOP concepts in PHP, including encapsulation, inheritance, polymorphism, and object interactions. Completing them will enhance your PHP development skills and prepare you for real-world applications.
Happy Coding! 🚀