PHP Classes

Gazer Simple PHP Observer Pattern Implementation: Register observers to handle subject object events

Recommend this page to a friend!
  Info   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 35 All time: 10,566 This week: 189Up
Version License PHP version Categories
gazer 1.0GNU General Publi...5PHP 5, Language, Design Patterns
Description Author

This package can register observers to handle subject object events.

It provides a base subject base class that can be extended by applications to provide subject implementation classes.

The package also provides an observer base class that can also be extended by applications to observe events that happen on subject objects.

Applications can attach observer objects to given subject objects.

When applications call subject classes to notify of given events, these classes will call all the attached observer classes to trigger any custom actions relevant to the applications.

Picture of Carlos Artur Curvelo da Matos
  Performance   Level  
Name: Carlos Artur Curvelo da ... <contact>
Classes: 19 packages by
Country: Portugal Portugal
Innovation award
Innovation award
Nominee: 13x

Winner: 2x

Details

Gazer

An easier and practical implementation of the Observer pattern in PHP. Work with abstracts rather than implementing interfaces.

Installation

composer require carloswph/gazer

Usage

Simple: instead of creating yourself abstracts or implementing pairs SplSubject and SplObserver in multiple classes, all you need to do is extending the classes Gazer\Subject and Gazer\Gazer in your subject class and the observers, respectivelly. Let's use a quick example:

use Gazer\Subject;
use Gazer\Gazer;

require __DIR__ . '/vendor/autoload.php';

// This is the subject class
class A extends Subject {

}
// This is one observer - but you could use the same logic
// for more observers, like classes C or D, for instance
class B extends Gazer {

}

At this moment, the classes A and B area already able to perform their roles in the observer pattern. Logic follows the pattern regular interfaces SplSubject and SplObserver, so the methods will be exactly the ones you expected.

$a = new A();
$b = new B();

// Now, all we need to do for B to "observer" A
// is using the method attach() from A

$a->attach($b);

// Ready! If A uses the method notify(), then a method
// update() in B will be ran, and B gets the A object
// held in the variable $subject, thus...

$a->notify(); // B gets A object immediately

Let's use the same classes, but now with some methods and properties. We'll create a property $example in A and a random method in B, which will apply any logic using the native property $subject.

use Gazer\Subject;
use Gazer\Gazer;

require __DIR__ . '/vendor/autoload.php';

class A extends Subject {

	public $example = 'Observed';

	public function __construct() {

		echo "Subject\n";
	}
}

class B extends Gazer {

	public function info() {

		echo $this->subject->example;
	}

}

$a = new A();
$b = new B();

// The only thing you can see is the word "Subject" echoed
// But now, let's put B to observe A

$a->attach($b);
$a->notify();

// As B is observing and A has notified all observers, so
// now we can manipulate the A object, which has been updated
// and held in the property $subject of B

$b->info(); // Prints "Observed"

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Gazer.php Class Class source
  Plain text file Subject.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:35
This week:0
All time:10,566
This week:189Up
For more information send a message to info at phpclasses dot org.