{"id":20162482,"url":"https://github.com/willavelar/php-design-pattern-behavioral-observer","last_synced_at":"2025-10-10T19:34:21.114Z","repository":{"id":204767756,"uuid":"712104329","full_name":"willavelar/php-design-pattern-behavioral-observer","owner":"willavelar","description":"a simple php example about the behavioral pattern: Observer","archived":false,"fork":false,"pushed_at":"2023-10-31T19:35:23.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T02:45:04.411Z","etag":null,"topics":["behavioral-patterns","design-patterns","observer","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willavelar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-30T19:53:07.000Z","updated_at":"2023-10-31T22:11:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"3de9b2b8-0506-4c84-909f-d771e0bd6b5c","html_url":"https://github.com/willavelar/php-design-pattern-behavioral-observer","commit_stats":null,"previous_names":["willavelar/php-design-partner-behavioral-observer","willavelar/php-design-pattern-behavioral-observer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/willavelar/php-design-pattern-behavioral-observer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willavelar","download_url":"https://codeload.github.com/willavelar/php-design-pattern-behavioral-observer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-observer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279005030,"owners_count":26083827,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["behavioral-patterns","design-patterns","observer","php"],"created_at":"2024-11-14T00:25:19.763Z","updated_at":"2025-10-10T19:34:21.096Z","avatar_url":"https://github.com/willavelar.png","language":"PHP","readme":"## Command\r\n\r\nObserver is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.\r\n\r\n-----\r\n\r\nWhen an order is generated, we need to perform several actions such as storing it in the database, sending it by email, and generating a log.\r\n\r\n### The problem\r\n\r\nIf we need to add more actions after generating order, our function will grow infinitely.\r\n\r\n```php\r\n\u003c?php\r\nclass Order\r\n{\r\n    public string $customerName;\r\n    public \\DateTimeInterface $finalizationDate;\r\n    public Budget $budget;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass Budget \r\n{\r\n    public float $value;\r\n    public int $items;\r\n}\r\n```\r\n\r\n```php\r\n\u003c?php\r\nclass GenerateOrder\r\n{\r\n    private float $budgetValue;\r\n    private int $items;\r\n    private string $customereName;\r\n\r\n    public function __construct(float $budgetValue, int $items, string $customereName)\r\n    {\r\n        $this-\u003ebudgetValue = $budgetValue;\r\n        $this-\u003eitems = $items;\r\n        $this-\u003ecustomereName = $customereName;\r\n    }\r\n\r\n    public function getBudgetValue(): float\r\n    {\r\n        return $this-\u003ebudgetValue;\r\n    }\r\n\r\n    public function getItems(): int\r\n    {\r\n        return $this-\u003eitems;\r\n    }\r\n\r\n    public function getCustomereName(): string\r\n    {\r\n        return $this-\u003ecustomereName;\r\n    }\r\n\r\n}\r\n```\r\n\r\n```php\r\n\u003c?php\r\nclass GenerateOrderHandler\r\n{\r\n    public function execute(GenerateOrder $generateOrder)\r\n    {\r\n        $budget =  new Budget();\r\n\r\n        $budget-\u003eitems = $generateOrder-\u003egetItems();\r\n        $budget-\u003evalue = $generateOrder-\u003egetBudgetValue();\r\n\r\n        $order = new Order();\r\n        $order-\u003efinalizationDate = new \\DateTimeImmutable();\r\n        $order-\u003ecustomerName = $generateOrder-\u003egetCustomereName();\r\n        $order-\u003ebudget = $budget;\r\n\r\n        new CreateOrderDatabase($order);\r\n        new GenerateOrderLog($order);\r\n        new SendOrderEmail($order);\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass CreateOrderDatabase\r\n{\r\n    public function __construct(Order $order)\r\n    {\r\n        echo \"create new order in database\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n```php\r\nclass GenerateOrderLog\r\n{\r\n    public function __construct(Order $order)\r\n    {\r\n        echo \"generate order creation log\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass SendOrderEmail\r\n{\r\n    public function __construct(Order $order)\r\n    {\r\n        echo \"send email to customer\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n### The solution\r\n\r\nNow, using the Observer design patern, we can step outside the class as many actions as we want, without changing it.\r\n```php\r\n\u003c?php\r\ninterface ActionsAfterGenerateOrder\r\n{\r\n    public function execAction(Order $order) : void;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass CreateOrderDatabase implements ActionsAfterGenerateOrder\r\n{\r\n    public function execAction(Order $order): void\r\n    {\r\n        echo \"create new order in database\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass GenerateOrderLog implements ActionsAfterGenerateOrder\r\n{\r\n    public function execAction(Order $order): void\r\n    {\r\n        echo \"generate order creation log\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass SendOrderEmail implements ActionsAfterGenerateOrder\r\n{\r\n    public function execAction(Order $order): void\r\n    {\r\n        echo \"send email to customer\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass GenerateOrderHandler\r\n{\r\n    /** @var ActionsAfterGenerateOrder[]  */\r\n    private array $actionsAfterGenerateOrder = [];\r\n\r\n    public function addActionsAfterGenerateOrder(ActionsAfterGenerateOrder $action)\r\n    {\r\n        $this-\u003eactionsAfterGenerateOrder[] = $action;\r\n    }\r\n    public function execute(GenerateOrder $generateOrder)\r\n    {\r\n        $budget =  new Budget();\r\n\r\n        $budget-\u003eitems = $generateOrder-\u003egetItems();\r\n        $budget-\u003evalue = $generateOrder-\u003egetBudgetValue();\r\n\r\n        $order = new Order();\r\n        $order-\u003efinalizationDate = new \\DateTimeImmutable();\r\n        $order-\u003ecustomerName = $generateOrder-\u003egetCustomereName();\r\n        $order-\u003ebudget = $budget;\r\n\r\n        foreach ($this-\u003eactionsAfterGenerateOrder as $action) {\r\n            $action-\u003eexecAction($order);\r\n        }\r\n    }\r\n}\r\n```\r\n-----\r\n\r\n### Installation for test\r\n\r\n![PHP Version Support](https://img.shields.io/badge/php-7.4%2B-brightgreen.svg?style=flat-square) ![Composer Version Support](https://img.shields.io/badge/composer-2.2.9%2B-brightgreen.svg?style=flat-square)\r\n\r\n```bash\r\ncomposer install\r\n```\r\n\r\n```bash\r\nphp wrong/test.php {budgetValue} {items} {customereName}\r\nphp right/test.php {budgetValue} {items} {customereName}\r\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-observer/lists"}