php 示例

最后更新于:2022-04-02 04:19:02

[TOC] ## 示例
index.php ``` usefulFunctionA(); return $result; } } class ConcreteProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return $result; } } function clientCode(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productB->usefulFunctionB() . "\n"; echo $productB->anotherUsefulFunctionB($productA) . "\n"; } clientCode(new ConcreteFactory1()); echo "\n"; clientCode(new ConcreteFactory2()); ```

输出 ``` The result of the product B1. The result of the product A1. The result of the product B2. The result of the product A2. ``` ### 渲染引擎示例
index.php ``` createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new TwigRenderer(); } } 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(); } } // 抽象产品 interface TitleTemplate { public function getTemplateString(): string; } class TwigTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "

{{ title }}

"; } } class PHPTemplateTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "

"; } } // 抽象产品 interface PageTemplate { public function getTemplateString(): string; } abstract class BasePageTemplate implements PageTemplate { protected $titleTemplate; public function __construct(TitleTemplate $titleTemplate) { $this->titleTemplate = $titleTemplate; } } class TwigPageTemplate extends BasePageTemplate { public function getTemplateString(): string { $renderedTitle = $this->titleTemplate->getTemplateString(); return << $renderedTitle
{{ content }}
HTML; } } class PHPTemplatePageTemplate extends BasePageTemplate { public function getTemplateString(): string { $renderedTitle = $this->titleTemplate->getTemplateString(); return << $renderedTitle
HTML; } } interface TemplateRenderer { public function render(string $templateString, array $arguments = []): string; } class TwigRenderer implements TemplateRenderer { public function render(string $templateString, array $arguments = []): string { return \Twig::render($templateString, $arguments); } } class PHPTemplateRenderer implements TemplateRenderer { public function render(string $templateString, array $arguments = []): string { extract($arguments); ob_start(); eval(' ?>' . $templateString . 'title = $title; $this->content = $content; } public function render(TemplateFactory $factory): string { $pageTemplate = $factory->createPageTemplate(); $renderer = $factory->getRenderer(); return $renderer->render($pageTemplate->getTemplateString(), [ 'title' => $this->title, 'content' => $this->content ]); } } $page = new Page('Sample page', 'This it the body.'); echo $page->render(new PHPTemplateFactory()); ```
输出 ```

Sample page

This it the body.
% ```