design-patterns/resources/view/patterns/observer.php

35 lines
861 B
PHP

<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-06
* @time: 15:35
*/
declare(strict_types = 1);
use Pattern\Behavioral\Observer\User;
use Pattern\Behavioral\Observer\UserObserver;
$user = new User();
/** These names will be skipped and not be added to the log of names */
$user->setName("Just a name");
$user->setName("Some new name");
$userObserver = new UserObserver();
$user->attach($userObserver);
$user->setName("Another new name");
$user->setName("Once again new name");
$user->setName("Possibly other new name");
/** @noinspection ForgottenDebugOutputInspection */
dump(
getenv('APP_ENV'),
["Another new name", "Once again new name", "Possibly other new name"],
$userObserver->getNamesLog(),
["Another new name", "Once again new name", "Possibly other new name"] === $userObserver->getNamesLog()
);