PHPackages                             phpninjas/observable - 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. phpninjas/observable

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

phpninjas/observable
====================

Notifier trait for making objects listenable.

v0.1.2(11y ago)0857ApachePHPPHP &gt;=5.4.0

Since Dec 18Pushed 11y ago1 watchersCompare

[ Source](https://github.com/phpninjas/Observable)[ Packagist](https://packagist.org/packages/phpninjas/observable)[ Docs](https://github.com/phpninjas/Observable)[ RSS](/packages/phpninjas-observable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

I got an event!
===============

[](#i-got-an-event)

Most modern php frameworks are migrating toward event and message based paradigms. This library goes some way to solving the problem of event object contracts between producers and subscribers.

Installation
============

[](#installation)

Get Composer

```
curl -sS https://getcomposer.org/installer | php
php composer.phar install
```

composer.json

```
{
  require: {
    "phpninjas/observable": "dev-master"
  }
}
```

Example
=======

[](#example)

Use the notifier in your class.

```
use Observable\Notifier;

class MyClass {
  use Notifier;

  public function doSomething(){
    $this->notifyObservers("did something");
  }

  public function doSomeEvent(){
    $this->notifyObservers(new MyEvent(1,2));
  }
}

$newClass = new MyClass();
$newClass->addObserver(function($expect){
  echo "got $expect";
});
$newClass->addObserver(function(MyObject $o){
  echo "got an object this time";
});

// runtime
$newClass->doSomething();
$newClass->doSomeEvent();
```

Caveats
=======

[](#caveats)

The notifier doesn't accept more than 1 argument to be notified of. This forces people to encapsulate their messages better, i.e. if you want to pass more than 1 piece of data encapsulate it in another object

```
class MyEvent {
  public function __construct($thing1, $things2){
    $this->thing1 = $thing1;
    $this->thing2 = $thing2;
  }
}

$newClass = new MyClass();
$newClass->addObserver(function(MyEvent $e){
  // i will ONLY get MyEvent objects, everything else will be ignored for me! YAY!
});
```

Bear in mind non-typesafe observers will receive EVERYTHING, If you add an observer with no typed parameter definition (i.e. the argument is not of a class Type) it will get ALL event messages.

Dragons
=======

[](#dragons)

Recursive events are bad! Don't do it. i.e. don't have an observer send the same event it just received to the same producer

```
// DO NOT DO THIS!
$observable = new MyClass();
$observable->addObserver(function(\MyEvent)use($observable){
  // this will recurse infinitely (or until stack overflow).
  $observable->doSomeEvent();
});

$observable->doSomeEvent();
```

Performance
===========

[](#performance)

The performance is roughly 50% faster than Symfony Dispatcher.

In a 2million event based test, Symfony event dispatcher took 37 seconds, while Notifier took 22 seconds (OSX 10.9.5, 2.3Ghz Intel I7, 16GB mem PHP 5.4.30).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~27 days

Total

2

Last Release

4141d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/460cc058bda3ffbaade325c31806d0c9a677e9f605f22b1d191485d24677c8e3?d=identicon)[james.turner.phpninja@gmail.com](/maintainers/james.turner.phpninja@gmail.com)

---

Top Contributors

[![james-turner](https://avatars.githubusercontent.com/u/437893?v=4)](https://github.com/james-turner "james-turner (12 commits)")

---

Tags

notifierobservabledispatcher

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phpninjas-observable/health.svg)

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

###  Alternatives

[realrashid/sweet-alert

Laravel Sweet Alert Is A Package For Laravel Provides An Easy Way To Display Alert Messages Using The SweetAlert2 Library.

1.2k2.9M21](/packages/realrashid-sweet-alert)[contributte/event-dispatcher

Best event dispatcher / event manager / event emitter for Nette Framework

292.4M19](/packages/contributte-event-dispatcher)[aura/dispatcher

Creates objects from a factory and invokes methods using named parameters; also provides a trait for invoking closures and object methods with named parameters.

3695.0k3](/packages/aura-dispatcher)

PHPackages © 2026

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