Home > Backend Development > PHP Tutorial > How to load PHP on demand to increase the flexibility of the program

How to load PHP on demand to increase the flexibility of the program

藏色散人
Release: 2023-04-08 12:12:02
forward
2822 people have browsed it

How to load PHP on demand to increase the flexibility of the program

I have basically forgotten the naming of design patterns, so I will call what I am describing now on-demand loading.

Requirements:

1. I hope to have a configuration file reading and writing class, which can be extended without modifying the original configuration file reading and writing class;

2. This extension is for example, my original configuration is in txt format, but now my configuration class is php or xml, etc., or it may be json

3. The calling interface is unified, no matter what type configuration file, I just call the same file to configure the read-write class to prevent subsequent code from being difficult to maintain.

Then:

1. First of all, the first thing that comes to mind is to define an abstract class and continuously inherit it. Through inheritance, there is no need to modify the configuration file reading and writing class;

2. However, I cannot use this configuration file to read the class uniformly. What I call is the class I inherited;

Implementation idea:

Okay, so much nonsense, let me talk about my implementation idea here. In fact, the whole idea is quite simple;

/**
 * 定义配置文件读写类,所有的配置文件读写调用此类就可以了,统一接口
 */
class Config {
    // 读
    public function read($file,$type = 'txt') {
        $instance = $this->getInstance($type);
        $instance->read($file);
    }
    // 写
    public function write($file,$type = 'txt') {
        $instance = $this->getInstance($type);
        $instance->read($file);
    }
    // 删
    public function delete($file,$type = 'txt') {
        $instance = $this->getInstance($type);
        $instance->read($file);
    }
    // 获取实际操作对象实例
    public function getInstance($type = 'txt') {
        $class_name = ucfirst($type).'Config'; // 根据文件格式实例化具体的操作类
        if(class_exists($class_name)) {
            $instance = new $class_name;
        } else {
            throw new Exception('未定义'.$class_name);
        }
        if(is_subclass_of($instance,'BaseConfig') !== 1) {
            throw new Exception('配置文件读写类必须继承BaseConfig');
        }
        return $instance;
    }
}
// 定义一个基础操作接口类,后续的文件读写必须继承这个规范
abstract class BaseConfig {
    abstract protected function read($file) {}
    abstract protected function write($file) {}
    abstract protected function delete($file) {}
}
// Text配置文件读写类
TxtConfig extends BaseConfig {
    public function read($file) {}
    public function write($file) {}
    public function delete($file) {}
}
// 其他配置文件读写类。。。
Copy after login

I have not tested the above code, what I expressed is just a Of course, based on this idea, a more flexible design can be made. An array configuration can be added to define which class to use for different files to read and write, and the time relationship. This issue will be updated later when there is time.

For more php related knowledge, please visit php tutorial!

The above is the detailed content of How to load PHP on demand to increase the flexibility of the program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template