This project helps you understand Object-Oriented Programming (OOP) and design patterns through simple, real-world examples.
Think of OOP like building with LEGO blocks:
- Each block (class) has its own shape and color
- You can combine blocks to make bigger things
- Some blocks can inherit features from others
- You can use blocks in different ways
- Class = Recipe (tells how to make something)
- Object = The actual thing made from the recipe
- Example:
class Car { } // Recipe for a car $myCar = new Car(); // An actual car
- Child classes get features from parent classes
- Like children inheriting traits from parents
- Example:
class Vehicle { } // Parent class class Car extends Vehicle { } // Child class
- Keeping things private inside a class
- Like a car's engine - you don't need to see it to drive
- Example:
class Car { private $engine; // Hidden from outside public function start() { } // Public method to use }
- Same method, different behavior
- Like different animals making different sounds
- Example:
class Animal { public function makeSound() { } } class Dog extends Animal { public function makeSound() { echo "Woof!"; } } class Cat extends Animal { public function makeSound() { echo "Meow!"; } }
- Showing only what's needed
- Like a TV remote - buttons are simple, but the inside is complex
- Example:
abstract class Remote { abstract public function powerButton(); }
Design patterns are like proven solutions to common problems.
- Problem: Creating objects in different ways
- Solution: A factory that makes objects for you
- Real Example: Different types of documents (PDF, Word)
- Code Example:
$document = DocumentFactory::create('pdf'); $document->save();
- Problem: Different ways to do the same thing
- Solution: Switch strategies easily
- Real Example: Different payment methods
- Code Example:
$payment = new PaymentProcessor(); $payment->setMethod(new CreditCard()); $payment->pay(100);
- Problem: Keeping objects updated
- Solution: Subscribe to changes
- Real Example: News notifications
- Code Example:
$news = new NewsPublisher(); $news->subscribe(new EmailSubscriber()); $news->publish("New update!");
- Problem: Adding features without changing code
- Solution: Wrap objects with new features
- Real Example: Coffee with toppings
- Code Example:
$coffee = new SimpleCoffee(); $coffee = new MilkDecorator($coffee); $coffee = new SugarDecorator($coffee);
- Problem: Need only one instance
- Solution: Control object creation
- Real Example: Application settings
- Code Example:
$settings = Settings::getInstance(); $settings->set('theme', 'dark');
- Install PHP on your computer
- Look at the examples in each file
- Try changing the code to see what happens
- Create your own examples using these patterns
- Start with simple examples
- Try to think of real-world uses
- Don't worry about memorizing - focus on understanding
- Practice by creating your own examples
Remember: Design patterns are tools, not rules. Use them when they help solve your problem!