PHP design mode registry mode (registration of multiple classes) _ PHP Tutorial

Source: Internet
Author: User
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...

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.