PHPackages                             phine/observer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phine/observer

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

phine/observer
==============

A PHP library that implements the observer pattern.

2.0.1(12y ago)5176.0k↑19.1%3MITPHPPHP &gt;=5.3.9

Since Sep 20Pushed 12y agoCompare

[ Source](https://github.com/kherge-archive/lib-observer)[ Packagist](https://packagist.org/packages/phine/observer)[ Docs](https://github.com/phine/lib-observer)[ RSS](/packages/phine-observer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (6)Used By (3)

Observer
========

[](#observer)

[![Build Status](https://camo.githubusercontent.com/cd06e978fa98a0735ac0afb323b4ae586201eaa4d7802f640c8a90a78f2e53d7/68747470733a2f2f7472617669732d63692e6f72672f7068696e652f6c69622d6f627365727665722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/phine/lib-observer)[![Coverage Status](https://camo.githubusercontent.com/00003c513cef92d2c2b0de0a615c1290c8cb51cd7c2e0d276a93ed50ff0ec199/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7068696e652f6c69622d6f627365727665722f62616467652e706e67)](https://coveralls.io/r/phine/lib-observer)[![Latest Stable Version](https://camo.githubusercontent.com/437e7eeeb7310796f4f3ba832a6c099cb8d1acfc14fe766c7060536f90298757/68747470733a2f2f706f7365722e707567782e6f72672f7068696e652f6f627365727665722f762f737461626c652e706e67)](https://packagist.org/packages/phine/observer)[![Total Downloads](https://camo.githubusercontent.com/ecd72334f07a481491e2c9898aaadbf467cc3d0f2c4a7e633b5e4563a9b2b38d/68747470733a2f2f706f7365722e707567782e6f72672f7068696e652f6f627365727665722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/phine/observer)

A PHP library that implements the observer pattern.

Summary
-------

[](#summary)

This library provides an implementation of the [observer pattern](http://en.wikipedia.org/wiki/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
-----------

[](#requirement)

- PHP &gt;= 5.3.9
- [Phine Exception](https://github.com/phine/lib-exception) &gt;= 1.0.0

Installation
------------

[](#installation)

Via [Composer](http://getcomposer.org/):

```
$ composer require "phine/observer=~2.0"

```

Usage
-----

[](#usage)

To create a subject, you will need to either create your own implementation of `SubjectInterface`, or use the bundled `Subject` class.

```
use Phine\Observer\Subject;

$subject = new Subject();
```

### Observing

[](#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.

```
use Phine\Observer\ObserverInterface;
use Phine\Observer\SubjectInterface;

// register a few instances
$subject->registerObserver(new Message('First'));
$subject->registerObserver(new Message('Second'));

// register the same one twice
$reuse = new Message('Third');

$subject->registerObserver($reuse);
$subject->registerObserver($reuse);

// notify all observers of an update
$subject->notifyObservers();

/**
 * Simply echos a message when updated.
 */
class Message implements ObserverInterface
{
    /**
     * The message to echo.
     *
     * @var string
     */
    private $message;

    /**
     * Sets the message to echo on update.
     *
     * @param string $message The message.
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * {@inheritDoc}
     */
    public function receiveUpdate(SubjectInterface $subject)
    {
        echo $this->message, "\n";
    }
}
```

With the example above, you can expect the following output:

```
First
Second
Third
Third

```

### Prioritizing Observers

[](#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:

```
$subject->registerObserver(new Message('A'), SubjectInterface::LAST_PRIORITY);
$subject->registerObserver(new Message('B'), 789);
$subject->registerObserver(new Message('C'), 123);
$subject->registerObserver(new Message('D'), 456);
$subject->registerObserver(new Message('E'), SubjectInterface::FIRST_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

[](#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.

```
use Phine\Observer\Exception\ReasonException;

/**
 * Simply interrupts the subject in the middle of an update.
 */
class InterruptingCow implements ObserverInterface
{
    /**
     * Interrupts the update.
     */
    public function receiveUpdate(SubjectInterface $subject)
    {
        // do some work

        $subject->interruptUpdate(
            new ReasonException('MOOOOO')
        );

        // do some final work
    }
}
```

Using the following example:

```
// create a new subject
$subject = new Subject();

// register some observers
$subject->registerObserver(new Message('So what did the interrupt cow say?'));
$subject->registerObserver(new InterruptingCow());
$subject->registerObserver(new Message('We never get this far.'));

// notify the observers
$subject->notifyObservers();
```

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

[](#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.

```
use Phine\Observer\Collection;

// create a new collection
$collection = new Collection();

// register a few subjects
$collection->registerSubject('one', new Subject());
$collection->registerSubject('two', new Subject());
$collection->registerSubject('three', new Subject());
```

You can then retrieve the subjects or replace them as needed.

```
// replace one
$collection->registerSubject('two', new Subject());

// update observers of another
$collection->getSubject('three')->notifyObservers();
```

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`.

```
use Phine\Observer\ArrayCollection;

// create a new collection
$collection = new ArrayCollection();

// register a few subjects
$collection['one'] = new Subject();
$collection['two'] = new Subject();
$collection['three'] = new Subject();
```

Like the regular `Collection` class, you can also replace and retrieve individual subjects.

```
// replace one
$collection['two'] = new Subject();

// update observers of another
$collection['three']->notifyObservers();
```

Documentation
-------------

[](#documentation)

You can find the [API documentation here](http://phine.github.io/lib-observer).

License
-------

[](#license)

This library is available under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Total

5

Last Release

4535d ago

Major Versions

1.0.x-dev → 2.0.02013-12-06

PHP version history (2 changes)1.0.0PHP &gt;=5.3.3

2.0.1PHP &gt;=5.3.9

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9122157?v=4)[Kevin Herrera](/maintainers/kherge)[@kherge](https://github.com/kherge)

---

Top Contributors

[![kherge](https://avatars.githubusercontent.com/u/9122157?v=4)](https://github.com/kherge "kherge (36 commits)")

---

Tags

observer

### Embed Badge

![Health badge](/badges/phine-observer/health.svg)

```
[![Health](https://phpackages.com/badges/phine-observer/health.svg)](https://phpackages.com/packages/phine-observer)
```

###  Alternatives

[jbzoo/event

Library for event-based development

29760.0k5](/packages/jbzoo-event)[akaunting/laravel-mutable-observer

Mutable observer package for Laravel

11173.5k](/packages/akaunting-laravel-mutable-observer)[mckenziearts/laravel-command

A simple Laravel package to provide artisan new commands

321.2k](/packages/mckenziearts-laravel-command)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
