PHP Observer mode implementation code _php tips

Source: Internet
Author: User

The code looks like this:

Copy Code code as follows:

The abstract class of the person being observed
Class observed implements splsubject{
protected $_name;
protected $_observers;

Instantiated to generate an observer object
Public Function __construct () {
$this->_observers = new Splobjectstorage ();
}

Add Observer Object
Public function Attach (Splobserver $observer) {
$this->_observers->attach ($observer);
}

Delete a Viewer object
Public Function Detach (Splobserver $observer) {
$this->_observers->detach ($observer);
}

Notification message
Public Function notify () {
foreach ($this->_observers as $observer) {
$observer->showmessage ($this);
}
}

Normal method: Setting a value
Public Function SetName ($name) {
$this->_name = $name;
$this->notify ();
}

Normal method: Get the value
Public Function GetName () {
return $this->_name;
}

Common method: Set age
Public Function Setage ($age) {
$this->age = $age;
foreach ($this->_observers as $observer) {
$observer->showage ($this->_name, $this->age);
}
}

}

Observer abstract class
Class Observer implements splobserver{

Show Message Hints
Public Function ShowMessage (Splsubject $obj) {
$user = $obj->getname ();
if ($user = = ' admin ') {
echo ' Hello, ', $user, ' welcome you into admin <br/> ';
}else{
echo "Hello, ' $user ' you have been added to the user list <br/>";
}
}
This is an abstract method that inherits the parent class
Public Function Update (Splsubject $subject) {}

Show personal Age
Public Function Showage ($name, $age) {
echo "<script>alert (' $name Age: $age ') </script>";
}
}

$subject = new observed (); To generate a observed object
$observer = new Observer (); Generate an Observer object
$subject->attach ($observer);//pass the observer to the observed
$subject->setname (' John '); Calling the SetName method
/*
* $this->notify () will be invoked through the setname of the surface;
* $observer->showmessage ($this) method is invoked by calling $this->notify ().
* i.e. the ShowMessage ($obj) method of each observer object;
*/
$subject->setname (' admin ');
$subject->setage (24);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.