--- url: "https://refactoring.guru/design-patterns/abstract-factory/php/example" titulek: "Abstract Factory in PHP / Design Patterns" h1: "Abstract Factory in PHP" popis: "Abstract Factory pattern in PHP. Full code example in PHP with detailed comments and explanation. Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/abstract-factory/php/example" drobecky: ["Home", "Design Patterns", "Abstract Factory", "PHP"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:27:04" znaku: 13491 slov: 1840 kodovani: "utf-8" sha256: "5405234cc18bcd8ca836418946152bc325bf5fa2d49537650fc08e6613cabc9b" --- # **Abstract Factory** in PHP **Abstract Factory** is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes. Abstract Factory defines an interface for creating all distinct products but leaves the actual product creation to concrete factory classes. Each factory type corresponds to a certain product variety. The client code calls the creation methods of a factory object instead of creating products directly with a constructor call (`new` operator). Since a factory corresponds to a single product variant, all its products will be compatible. Client code works with factories and products only through their abstract interfaces. This lets the client code work with any product variants, created by the factory object. You just create a new concrete factory class and pass it to the client code. > If you can’t figure out the difference between various factory patterns and concepts, then read our [Factory Comparison](/design-patterns/factory-comparison). [Learn more about Abstract Factory](/design-patterns/abstract-factory) **Complexity:** **Popularity:** **Usage examples:** The Abstract Factory pattern is pretty common in PHP code. Many frameworks and libraries use it to provide a way to extend and customize their standard components. **Identification:** The pattern is easy to recognize by methods, which return a factory object. Then, the factory is used for creating specific sub-components. ## Conceptual Example This example illustrates the structure of the **Abstract Factory** design pattern. It focuses on answering these questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case. #### **index.php:** Conceptual example ``` usefulFunctionA(); return "The result of the B1 collaborating with the ({$result})"; } } class ConcreteProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } /** * The variant, Product B2, is only able to work correctly with the variant, * Product A2. Nevertheless, it accepts any instance of AbstractProductA as * an argument. */ public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return "The result of the B2 collaborating with the ({$result})"; } } /** * The client code works with factories and products only through abstract * types: AbstractFactory and AbstractProduct. This lets you pass any factory or * product subclass to the client code without breaking it. */ function clientCode(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productB->usefulFunctionB() . "\n"; echo $productB->anotherUsefulFunctionB($productA) . "\n"; } /** * The client code can work with any concrete factory class. */ echo "Client: Testing client code with the first factory type:\n"; clientCode(new ConcreteFactory1()); echo "\n"; echo "Client: Testing the same client code with the second factory type:\n"; clientCode(new ConcreteFactory2()); ``` #### **Output.txt:** Execution result ``` Client: Testing client code with the first factory type: The result of the product B1. The result of the B1 collaborating with the (The result of the product A1.) Client: Testing the same client code with the second factory type: The result of the product B2. The result of the B2 collaborating with the (The result of the product A2.) ``` ## Real World Example In this example, the **Abstract Factory** pattern provides an infrastructure for creating various types of templates for different elements of a web page. A web application can support different rendering engines at the same time, but only if its classes are independent of the concrete classes of rendering engines. Hence, the application’s objects must communicate with template objects only via their abstract interfaces. Your code shouldn’t create the template objects directly, but delegate their creation to special factory objects. Finally, your code shouldn’t depend on the factory objects either but, instead, should work with them via the abstract factory interface. As a result, you will be able to provide the app with the factory object that corresponds to one of the rendering engines. All templates, created in the app, will be created by that factory and their type will match the type of the factory. If you decide to change the rendering engine, you’ll be able to pass a new factory to the client code, without breaking any existing code. #### **index.php:** Real world example ``` createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new TwigRenderer(); } } /** * And this Concrete Factory creates PHPTemplate templates. */ class PHPTemplateFactory implements TemplateFactory { public function createTitleTemplate(): TitleTemplate { return new PHPTemplateTitleTemplate(); } public function createPageTemplate(): PageTemplate { return new PHPTemplatePageTemplate($this->createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new PHPTemplateRenderer(); } } /** * Each distinct product type should have a separate interface. All variants of * the product must follow the same interface. * * For instance, this Abstract Product interface describes the behavior of page * title templates. */ interface TitleTemplate { public function getTemplateString(): string; } /** * This Concrete Product provides Twig page title templates. */ class TwigTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "