michaelking0 / observers
A basic implementation of the observer pattern in PHP
Installs: 18
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/michaelking0/observers
Requires (Dev)
- phpunit/phpunit: ^5.5
This package is auto-updated.
Last update: 2025-10-12 00:36:24 UTC
README
Basic PHP observer pattern implementation
Usage
Installation
composer require michaelking0/observers
Making a class Observable (the subject)
In your subject class, implement the MichaelKing0\Observers\Interfaces\ObservableInterface interface. Also, use the trait MichaelKing0\Observers\Traits\ObservableTrait.
<?php namespace ACME; use MichaelKing0\Observers\Interfaces\ObservableInterface; use MichaelKing0\Observers\Traits\ObservableTrait; class MySubject implements ObservableInterface { use ObservableTrait; }
Then just add a function that uses the notify method
<?php namespace ACME; use MichaelKing0\Observers\Interfaces\ObservableInterface; use MichaelKing0\Observers\Traits\ObservableTrait; class MySubject implements ObservableInterface { use ObservableTrait; public function save() { $this->notify('SubjectSaved'); } }
This will notify an observers of the event, and pass the event name and current instance as an argument
Creating an Observer
To create an observer, implement the MichaelKing0\Observers\Interfaces\ObservableInterface interface.
<?php namespace ACME; use MichaelKing0\Observers\Interfaces\ObservableInterface; use MichaelKing0\Observers\Interfaces\ObserverInterface; class MyObserver implements ObserverInterface { public function update($event, ObservableInterface $observable) { echo 'It works!'; } }
Attaching an observer
You can attach an observer to a subject by calling the attach method with the event name, and observer instance i.e.
$subject = new MySubject(); $subject->attach('SubjectSaved', new MyObserver());