PHP design mode registry mode (registration of multiple classes ). I have written a registry class before, but that one cannot register multiple classes. the classes are stored using arrays below. Copy the code as follows :? I have also written a registry class for the php base class classwebS before, but that class cannot be registered multiple classes. the classes are stored using arrays below.
The code is as follows:
// Basic class
Class webSite {// A very simple basic class
Private $ siteName;
Private $ siteUrl;
Function _ construct ($ siteName, $ siteUrl ){
$ This-> siteName = $ siteName;
$ This-> siteUrl = $ siteUrl;
}
Function getName (){
Return $ this-> siteName;
}
Function getUrl (){
Return $ this-> siteUrl;
}
}
Class registry {// registry class Singleton mode
Private static $ instance;
Private $ values = array (); // store the class name in an array
Private function _ construct () {}// this usage determines that this class cannot be directly instantiated.
Static function instance (){
If (! Isset (self ::$ instance) {self ::$ instance = new self ();}
Return self: $ instance;
}
Function get ($ key) {// get the registered class
If (isset ($ this-> values [$ key]) {
Return $ this-> values [$ key];
}
Return null;
}
Function set ($ key, $ value) {// registration class method
$ This-> values [$ key] = $ value;
}
}
$ Reg = registry: instance ();
$ Reg-> set ("website", new webSite ("WEB development notes", "www.chhua.com"); // register the class
$ Website = $ reg-> get ("website"); // Obtain the class
Echo $ website-> getName (); // output WEB Development notes
Echo $ website-> getUrl (); // output www.chhua.com
?>
The registry provides system-level object access. Some students say that this is an option. However, it is not necessary to register classes in small projects. it is useful for large projects.
Bytes. The code is as follows :? Php // Basic class webS...