1. Go to this page and download the library: Download phine/observer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phine / observer example snippets
use Phine\Observer\Subject;
$subject = new Subject();
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";
}
}
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
}
}
// 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();
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());
// replace one
$collection->registerSubject('two', new Subject());
// update observers of another
$collection->getSubject('three')->notifyObservers();
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();
// replace one
$collection['two'] = new Subject();
// update observers of another
$collection['three']->notifyObservers();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.