Php load on demand to increase program flexibility

Source: Internet
Author: User
: This article describes how to increase the flexibility of php programs by loading on demand. For more information about PHP tutorials, see. The name of the design pattern is almost forgotten. I just want to call this on demand.

Requirements:

1. I want to have a configuration file read/write class that can be extended without modifying the original configuration file read/write class;
2. this extension is like my original configuration is in txt format, but now my configuration class is php or xml, or it may be json
3. unified calling interfaces. no matter what type of configuration files, I can call the same file to configure the read/write class, preventing the subsequent code from being difficult to maintain.

So:

1. First of all, I think of defining an abstract class and continuing to inherit. through inheritance, I don't need to modify the configuration file read/write class;
2. However, I cannot use this configuration file to read classes in a unified manner. I am calling this class that I inherited;

Implementation idea:

Well, there is so much nonsense. here I will talk about my implementation ideas. In fact, the entire idea is quite simple;

/*** Defines the configuration file read/write class. you can call this class to read and write all configuration files. the unified interface */class Config {// reads public function read ($ file, $ type = 'txt ') {$ instance = $ this-> getInstance ($ type); $ instance-> read ($ file );} // write public function write ($ file, $ type = 'txt ') {$ instance = $ this-> getInstance ($ type ); $ instance-> read ($ file);} // delete public function delete ($ file, $ type = 'txt ') {$ instance = $ this-> getInstance ($ type); $ instance-> read ($ file );} // Obtain the public function getInstance ($ type = 'txt ') of the actual operation object instance {$ class_name = ucfirst ($ type ). 'config'; // instantiate the Operation class if (class_exists ($ class_name) {$ instance = new $ class_name;} else {throw new Exception ('undefined '. $ class_name);} if (is_subclass_of ($ instance, 'baseconfig ')! = 1) {throw new Exception ('configuration file read/write class must inherit baseconfig');} return $ instance ;}// defines a basic operation interface class, subsequent file reads and writes must inherit this specification abstract class BaseConfig {abstract protected function read ($ file) {}abstract protected function write ($ file) {} abstract protected function delete ($ file) {}}// Text configuration file read/write TxtConfig extends BaseConfig {public function read ($ file) {}public function write ($ file) {} public function delete ($ fil E) {}} // Other configuration file read/write classes...

I have not tested the above code. I only express an idea. of course, based on this idea, I can design more flexibly, you can add an array configuration to define the classes used for reading and writing different files. the time relationship will be updated later.

The above describes how to increase the flexibility of php programs by loading on demand, including some content, and hope to help those who are interested in PHP tutorials.

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.