0
0
PHPprogramming~10 mins

Factory pattern in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Factory pattern
Client calls Factory
Factory decides which class to create
Factory creates object
Client uses object
End
The client asks the factory to create an object. The factory decides which class to instantiate and returns the object. The client then uses the object.
Execution Sample
PHP
<?php
interface Animal {
  public function speak();
}

class Dog implements Animal {
  public function speak() { return "Woof!"; }
}

class Cat implements Animal {
  public function speak() { return "Meow!"; }
}

class AnimalFactory {
  public static function create($type): Animal {
    return match($type) {
      'dog' => new Dog(),
      'cat' => new Cat(),
      default => throw new Exception("Unknown animal type")
    };
  }
}

$animal = AnimalFactory::create('dog');
echo $animal->speak();
?>
This code uses a factory to create either a Dog or Cat object based on input, then calls speak() on it.
Execution Table
StepActionInputFactory DecisionObject CreatedOutput
1Client calls AnimalFactory::create'dog'Matches 'dog'Dog instance
2Dog->speak() calledWoof!
3Program ends
💡 Factory returns Dog object; speak() outputs 'Woof!'; program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$typeundefined'dog''dog''dog'
$animalundefinedDog instanceDog instanceDog instance
OutputnonenoneWoof!Woof!
Key Moments - 2 Insights
Why does the client not create Dog or Cat directly?
The client calls the factory (see Step 1 in execution_table) to avoid knowing the exact class. This makes code flexible and easier to change.
What happens if an unknown type is passed to the factory?
The factory throws an Exception (not shown in this trace). This prevents creating invalid objects and keeps code safe.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what object is created at Step 1?
ADog instance
BCat instance
CAnimal interface
DException
💡 Hint
Check the 'Object Created' column in Step 1 of execution_table.
At which step does the speak() method output 'Woof!'?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Output' column in execution_table for each step.
If the client called AnimalFactory::create('cat'), what would change in the execution_table?
AOutput would be 'Woof!'
BAn Exception would be thrown
CFactory Decision would be 'Matches cat' and Object Created would be Cat instance
DNo change
💡 Hint
Refer to the 'Factory Decision' and 'Object Created' columns in execution_table Step 1.
Concept Snapshot
Factory pattern creates objects without exposing creation logic.
Client calls factory with a type.
Factory returns an object of the correct class.
Helps keep code flexible and easy to extend.
Use static methods or classes to implement factory.
Full Transcript
The factory pattern lets a client ask a factory to create an object. The factory decides which class to instantiate based on input. In the example, the client calls AnimalFactory::create('dog'). The factory matches 'dog' and creates a Dog object. Then the client calls speak() on the Dog object, which outputs 'Woof!'. This way, the client does not need to know about Dog or Cat classes directly. If the client asked for 'cat', the factory would create a Cat object instead. This pattern helps keep code flexible and easier to maintain.