Php
/
Design Patterns
- 1 Basics 5
-
Quotes S
-
Constants S
-
Control structures S
-
Reference S
-
Number systems S
- 2 Variables 4
-
Definition S
-
Variable variable S
-
Exists S
-
Type casting S
- 3 Operators 5
-
Aritmetic S
-
Bitwise S
-
String S
-
Comparison S
-
Logical S
- 4 Function 4
-
Definition S
-
Anonymous S
-
Reference S
-
Variable arguments S
- 5 Array 7
-
Basics S
-
Operations S
-
Create S
-
Search S
-
Modify S
-
Sort S
-
Storage S
- 6 String 9
-
Basics
-
Compare
-
Search
-
Replace
-
Format
-
Regexp
-
Parse
-
Formating
-
Json
- 7 Streams 6
-
File open
-
Read file
-
Read csv
-
File contents
-
Context
-
Ob_start
- 8 Oop 6
-
Object instantiation S
-
Class constructor
-
Interfaces, abstract
-
Resource visibility
-
Class constants
-
Namespaces
- 9 Features 9
-
Autoload
-
Class reflection
-
Magic methods
-
Exceptions S
-
Late static binding
-
Type hinting
-
SPL
-
PHPUNIT
-
PHAR
- 10 Versions 2
-
Php7.4 S
-
Php8.0 S
- 11 Http 4
-
Headers
-
File Uploads
-
Cookies
-
Sessions
- 12 Design Patterns 4
-
Singleton Pattern S
-
Observer Pattern
-
Strategy Pattern
-
Registry
- 13 Modern Php 8
-
Composer S
-
Slim Framework
-
Autoloader
-
Package
-
Releases
-
Generators
-
Dependency Injection
-
Middleware
- 14 Create Framework 7
-
App
-
Http Foundation
-
Front Controller
-
Routing
-
Render Controller
-
Resolver
-
SoC
- 15 Frameworks 4
-
Symfony v5
-
Laravel v8
-
Laminas v3
-
Codeigniter v4
- 16 Composer 5
-
Guzzle
-
Carbon
-
Faker
-
Math
-
Requests
- 17 Symfony 6
-
Routes S
-
Annotations
-
Flex
-
Controllers
-
Doctrine
-
Templating
R
Q
Observer Pattern
It is used when we have several objects that are dependent of another object. When a Subject object changes its state, the attached Observers will be notified.
// index.php
namespace DesignPatterns\Observer;
require_once __DIR__ . "/../../autoloader.php";
$app = new App();
$guestModel = new AppGuestModel();
$userModel = new AppUserModel();
$app->attachObserver($guestModel);
$app->attachObserver($userModel);
$app->userId = 123;
$app->notifyObservers();
echo $guestModel->userId; // 123
echo $userModel->userId; // 123
copy
// autoload.php
function classAutoloader($class) {
$tmp = explode("\\", $class);
require_once join("/", $tmp) . ".php";
}
spl_autoload_register("classAutoloader");
Interfaces
// Subject.php
namespace DesignPatterns\Observer;
interface Subject {
public function attachObserver(Observer $observer);
public function detachObserver(Observer $observer);
public function notifyObservers();
}
// Observer.php
namespace DesignPatterns\Observer;
interface Observer {
public function update(Subject $subject);
}
Subject
copy
// App.php
namespace DesignPatterns\Observer;
class App implements Subject {
private $observers = [];
public $userId;
public function attachObserver(Observer $o) {
$this->observers[] = $o;
}
public function detachObserver(Observer $o) {
$this->observers = array_diff(
$this->observers, array($o)
);
}
public function notifyObservers() {
foreach($this->observers as $o) {
$o->update($this);
}
}
}
Observers
// AppUserModel.php
namespace DesignPatterns\Observer;
class AppUserModel extends AppBaseModel {
public $userId;
}
// AppGuestModel.php
namespace DesignPatterns\Observer;
class AppGuestModel extends AppBaseModel {
public $userId;
}
copy
// AppBaseModel.php
namespace DesignPatterns\Observer;
class AppBaseModel implements Observer {
// Override
public function update(Subject $subject) {
foreach(get_object_vars($subject) as $k=>$v) {
if (property_exists($this, $k)) {
$this->$k = $v;
}
}
}
}
➥ SPL Library
SPL Library
PHP already has standard libraries (SplObserver, SplSubject).
copy
// index.php
declare(strict_types=1);
namespace DesignPatterns\Observer;
require_once __DIR__ . "/../../autoloader.php";
$app = new App();
$guestModel = new AppGuestModel();
$userModel = new AppUserModel();
$app->attach($guestModel);
$app->attach($userModel);
$app->setUserId(123);
//$app->setUserId("123"); // throws error: must be nteger
echo $userModel->userId; // 123
echo $guestModel->userId; // 123
copy
// App.php
declare(strict_types=1);
namespace DesignPatterns\Observer;
use SplSubject;
use SplObserver;
use SplObjectStorage;
class App implements SplSubject
{
private $observers;
public $userId;
public function __construct()
{
$this->observers = new SplObjectStorage();
}
public function setUserId(int $userId): void
{
$this->userId = $userId;
$this->notify(); // notify observers
}
// @Override
public function attach(SplObserver $o)
{
$this->observers->attach($o);
}
// @Override
public function detach(SplObserver $o)
{
$this->observers->dettach($o);
}
// @Override
public function notify()
{
foreach($this->observers as $o) {
$o->update($this);
}
}
}
copy
// AppGuestModel.php
namespace DesignPatterns\Observer;
use SplSubject;
use SplObserver;
class AppGuestModel implements SplObserver
{
public $userId;
// @Override
public function update(SplSubject $subject)
{
$this->userId = $subject->userId;
}
}
copy
// AppUserModel.php
namespace DesignPatterns\Observer;
use SplSubject;
use SplObserver;
class AppUserModel implements SplObserver
{
public $userId;
// @Override
public function update(SplSubject $subject)
{
$this->userId = $subject->userId;
}
}
➥ Compact
Compact
Observer pattern in one file.
code
// Subject
class App implements SplSubject
{
private $observers;
public $userId;
public function __construct()
{
$this->observers = new SplObjectStorage();
}
public function setUserId(int $userId): void
{
$this->userId = $userId;
$this->notify(); // notify observers
}
// @Override
public function attach(SplObserver $o)
{
$this->observers->attach($o);
}
// @Override
public function detach(SplObserver $o)
{
$this->observers->dettach($o);
}
// @Override
public function notify()
{
foreach($this->observers as $o) {
$o->update($this);
}
}
}
// Observer
class AppGuestModel implements SplObserver
{
public $userId;
// @Override
public function update(SplSubject $subject)
{
$this->userId = $subject->userId;
}
}
// Observer
class AppUserModel implements SplObserver
{
public $userId;
// @Override
public function update(SplSubject $subject)
{
$this->userId = $subject->userId;
}
}
$app = new App();
$guestModel = new AppGuestModel();
$userModel = new AppUserModel();
$app->attach($guestModel);
$app->attach($userModel);
$app->setUserId(123);
echo $userModel->userId; // 123
echo $guestModel->userId; // 123
➥ Questions