Skip to content

Instantly share code, notes, and snippets.

@atakde
Created June 29, 2022 17:49
Show Gist options
  • Save atakde/95f0a6c1e40793f3f0953ea9fdfebceb to your computer and use it in GitHub Desktop.
Save atakde/95f0a6c1e40793f3f0953ea9fdfebceb to your computer and use it in GitHub Desktop.
Prototype Design Pattern PHP Example
<?php
abstract class MealPrototype
{
protected $mealName;
protected $mealCategory;
public function getMealName(): string
{
return $this->mealName;
}
public function setMealName(string $name): string
{
return $this->mealName = $name;
}
}
class VegetableMealPrototype extends MealPrototype
{
protected $mealCategory = "vegetable";
/**
* Clone..
*/
public function __clone()
{
}
}
class MeatMealPrototype extends MealPrototype
{
protected $mealCategory = "meat";
/**
* Clone..
*/
public function __clone()
{
}
}
$meatMealPrototype = new MeatMealPrototype();
$vegetableMealPrototype = new VegetableMealPrototype();
for ($i = 0; $i < 5; $i++) {
$object = clone $meatMealPrototype;
$object->setMealName('Meal Menu #' . $i);
echo $object->getMealName() . PHP_EOL;
}
for ($i = 0; $i < 5; $i++) {
$object = clone $vegetableMealPrototype;
$object->setMealName('Vegetable Menu #' . $i);
echo $object->getMealName() . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment