Server : Apache/2.4.43 (Win64) OpenSSL/1.1.1g PHP/7.4.6
System : Windows NT USER-PC 6.1 build 7601 (Windows 7 Professional Edition Service Pack 1) AMD64
User : User ( 0)
PHP Version : 7.4.6
Disable Function : NONE
Directory :  C:/xampp/php/pear/examples/
Upload File :
Current Directory [ Writeable ] Root Directory [ Writeable ]


Current File : C:/xampp/php/pear/examples/object.php
<?PHP
/**
 * example that show how to use objects as observers without
 * loosing references
 *
 * @package    Event_Dispatcher
 * @subpackage Examples
 * @author     Stephan Schmidt <schst@php.net>
 */

/**
 * load Event_Dispatcher package
 */
require_once 'Event/Dispatcher.php';

/**
 * example sender
 */
class sender
{
    var $_dispatcher = null;
    
    function sender(&$dispatcher)
    {
        $this->_dispatcher = &$dispatcher;
    }
    
    function foo()
    {
        $notification = &$this->_dispatcher->post($this, 'onFoo', 'Some Info...');
        echo "notification::foo is {$notification->foo}<br />";
    }
}

/**
 * example observer
 */
class receiver
{
    var $foo;
    
    function notify(&$notification)
    {
        echo "received notification<br />";
        echo "receiver::foo is {$this->foo}<br />";
        $notification->foo = 'bar';
    }
}

$dispatcher = &Event_Dispatcher::getInstance();

$sender = &new sender($dispatcher);
$receiver = new receiver();
$receiver->foo = 42;

// make sure you are using an ampersand here!
$dispatcher->addObserver(array(&$receiver, 'notify'));

$receiver->foo = 'bar';

echo 'sender->foo()<br />';
$sender->foo();
?>