, (*1)
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
., (*2)
<?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, (*3)
<?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, (*4)
Creating an Observer
To create an observer, implement the MichaelKing0\Observers\Interfaces\ObservableInterface
interface., (*5)
<?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., (*6)
$subject = new MySubject();
$subject->attach('SubjectSaved', new MyObserver());