I have written a registry class before, but that one cannot register multiple classes. The classes are stored using arrays below.
Copy codeThe Code is as follows: <? Php
// 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.