PHPackages                             jimjam88/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. jimjam88/observer

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

jimjam88/observer
=================

Observer pattern for PHP with C# like event arguments

34PHP

Since Sep 14Pushed 10y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Observer
========

[](#observer)

Very simple observer pattern for PHP with C# like event arguments.

Usage
-----

[](#usage)

Create a subject by extending the `Observer\Subject\SubjectAbstract` or by implementing the `Observer\Subject\SubjectInterface`...

```
use Observer\Subject\SubjectAbstract;

class Subject extends SubjectAbstract
{

}
```

Create an event that will be published to the observers. All event args **must** extend `Observer\Event\EventArgs`

```
use Observer\Event\CallbackEventArgs;

class Event extends CallbackEventArgs
{
    /**
     * Some event args info
     *
     * @var string
     */
    protected $info;

    /**
     * Gets the value of info.
     *
     * @return string
     */
    public function getInfo()
    {
        return $this->info;
    }

    /**
     * Sets the value of info.
     *
     * @param  string $info the info
     * @return self
     */
    public function setInfo($info)
    {
        $this->info = $info;

        return $this;
    }
}
```

Now create some observers. Observers must implement the `Observer\Observer\ObserverInterface`

```
use Observer\Observer\ObserverInterface;
use Observer\Event\EventArgs;

class ObserverA implements ObserverInterface
{
    public function notify(EventArgs $args)
    {
        var_dump(__CLASS__);

        var_dump($args->getInfo());

        call_user_func($args->getCallback());
    }
}

class ObserverB implements ObserverInterface
{
    public function notify(EventArgs $args)
    {
        var_dump(__CLASS__);

        var_dump($args->getInfo());

        call_user_func($args->getCallback());
    }
}
```

Now tie it altogether...

```
// Instantiate the subject
$subject = new Subject();

// ...and attach the observers
$subject->attach(new ObserverA());
$subject->attach(new ObserverB());

// Create an event
$event = new Event();
$event->setInfo('Hello, World!');
$event->setCallback(function() {
    var_dump('Hello, callback!');
});

// Notify the observers of the event
$subject->notify($event);
```

The above will output:

```
string(9) "ObserverA"
string(13) "Hello, World!"
string(16) "Hello, callback!"
string(9) "ObserverB"
string(13) "Hello, World!"
string(16) "Hello, callback!"
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e35ba5c2da58828ae134547415ead984dba9920245452d78d67602df729a767?d=identicon)[jimjam88](/maintainers/jimjam88)

---

Top Contributors

[![jimjam88](https://avatars.githubusercontent.com/u/1132487?v=4)](https://github.com/jimjam88 "jimjam88 (6 commits)")

### Embed Badge

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

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

PHPackages © 2026

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