PHP observer mode implementation code

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows:
// The observed abstract class
Class Observed implements SplSubject {
Protected $ _ name;
Protected $ _ observers;

// Instantiate to generate an observer object
Public function _ construct (){
$ This-> _ observers = new SplObjectStorage ();
}

// Add an observer object
Public function attach (SplObserver $ observer ){
$ This-> _ observers-> attach ($ observer );
}

// Delete the viewer object
Public function detach (SplObserver $ observer ){
$ This-> _ observers-> detach ($ observer );
}

// Notification message
Public function required y (){
Foreach ($ this-> _ observers as $ observer ){
$ Observer-> showMessage ($ this );
}
}

// Normal method: set the value
Public function setName ($ name ){
$ This-> _ name = $ name;
$ This-> Policy ();
}

// Normal method: Get the value
Public function getName (){
Return $ this-> _ name;
}

// Normal method: set the 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 {

// Display message prompts
Public function showMessage (SplSubject $ obj ){
$ User = $ obj-> getName ();
If ($ user = 'admin '){
Echo 'hello, ', $ user,' Welcome To The management background <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 ){}

// Display the personal age
Public function showAge ($ name, $ age ){
Echo "<script> alert ('$ name is $ age') </script> ";
}
}

$ Subject = new Observed (); // generates an object to be Observed.
$ Observer = new Observer (); // generates an observer object
$ Subject-> attach ($ observer); // transmits the observer to the observed object.
$ Subject-> setName ('zhang san'); // call the setName Method
/*
* The following setName will call $ this-> Policy ();
* By calling $ this-> notify (), the $ observer-> showMessage ($ this) method is called,
* This is 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.