Download the PHP package phine/observer without Composer
On this page you can find all versions of the php package phine/observer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phine/observer
More information about phine/observer
Files in phine/observer
Package observer
Short Description A PHP library that implements the observer pattern.
License MIT
Homepage https://github.com/phine/lib-observer
Informations about the package observer
Observer
A PHP library that implements the observer pattern.
Summary
This library provides an implementation of the observer pattern. You can use it to create other libraries such as event managers, state machines, MVC frameworks, and even provide a plugin system for application.
Requirement
- PHP >= 5.3.9
- Phine Exception >= 1.0.0
Installation
Via Composer:
$ composer require "phine/observer=~2.0"
Usage
To create a subject, you will need to either create your own implementation
of SubjectInterface
, or use the bundled Subject
class.
Observing
You will then need to create your own implementation of ObserverInterface
to observe changes made to the subject. You may use multiple instances of
the observer implementation, or even the same instance multiple times.
With the example above, you can expect the following output:
First
Second
Third
Third
Prioritizing Observers
Implementations of SubjectInterface
support prioritizing observers during
registration (registerObserver()
). By default, shown in all the examples
provided, the priority is SubjectInterface::FIRST_PRIORITY
(which is 0
,
zero). You may, however, specify your own priority:
With the above example, you can expect the following output:
E
C
D
B
A
When a subject updates its observers it begins at priority 0
(zero), and works
its way to PHP_INT_MAX
(the lowest possible priority). If multiple observers
are registered using the same provider, they will be updated in the order that
they were registered.
Interrupting an Update
When a subject is in the process of updating its registered observers, an
observer may interrupt the subject. An interrupt is performed by an observer
when it calls the SubjectInterface::interruptUpdate()
method.
Using the following example:
You can expect the following output:
So what did the interrupting cow say?
PHP Fatal error: Uncaught exception '[...]' with message 'MOOOOO' [...]
[...]
Observers are not required to provide a reason (instance of ReasonException
),
but it will definitely help during the debugging process if one is given.
Collections of Subjects
There may be occasions where you will need to manage a collection of subjects.
The library provides two ways of doing so: Collection
and ArrayCollection
.
The Collection
will associate an individual subject with a specific unique
identifier.
You can then retrieve the subjects or replace them as needed.
The ArrayCollection
class provides a leaner way of managing subject
registrations. It is an extension of the Collection
class that supports
array access through ArrayCollectionInterface
.
Like the regular Collection
class, you can also replace and retrieve
individual subjects.
Documentation
You can find the API documentation here.
License
This library is available under the MIT license.