PHP on-demand load mode to increase program flexibility

Source: Internet
Author: User
Design pattern naming Ah what, I basically have forgotten almost, I will now express this thing called on demand load it.

Demand:

1. I want to have a configuration file read-write class, do not need to modify the original configuration file read-write class can be extended;
2. This extension is for example my original configuration is txt format, but now my configuration class is PHP or XML, or it may be JSON
3. Call the interface unified, no matter what type of configuration file, I call the same file configuration read-write class can be, to prevent the subsequent code is difficult to maintain.

So:

1. First, think of the definition of an abstract class, continuous inheritance, through inheritance without modifying the configuration file read-write class;
2. However, I cannot use this configuration file to read the class in a uniform, I call this class after I inherit;

Realization idea:

Well, nonsense so much, I am here to say my idea of implementation, in fact, the whole idea is quite simple;

/** * Defines the configuration file read-write class, all the configuration file read-write call this class can be, unified interface */class Config {//Read public function read ($file, $type = ' txt ') {$ins        tance = $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); }//Gets the actual Action object instance public function getinstance ($type = ' txt ') {$class _name = Ucfirst ($type). ' Config ';        Instantiate the specific action class according to the file format 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 (' config file read-write class must inherit Baseconfig ');    } return $instance; }}//defines a basic operating interface class, and 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 config file read-write class Txtconfig extends Baseconfig {public function read ($file) {} Public Function write ($file) {} p ublic function Delete ($file) {}}//Other config file read-write class ...

I have not tested the above code, I Express is only a thought, of course, based on this idea can also be designed to be more flexible, you can add an array configuration to define the different files to use which class to read and write, time relationship, the problem of the subsequent time to update.

The above describes the PHP on-demand loading mode to increase the flexibility of the program, including the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.