Skip to content

FAQ baseExtends

tolizhan edited this page Apr 27, 2023 · 3 revisions

怎么在系统的基础上扩展底层功能

只需要添加新的模块

框架本身便是模块组成的

  1. 框架由五个核心文件组成
  2. 其它全部作为框架的模块而存在
  3. 通过调用底层事件(钩子)使框架得以运作
  4. 添加新模块便可添加新功能

配合四个功能添加新模块

  1. 扩展开发 可以写一些额外的小功能通过安装的方式改变系统的逻辑
  2. 预先加载可分两个, 一个是通过配置文件中的预先加载代码, 一个是通过of::link预先注册L类方法, 来实现一些新功能
  3. 还可通过事件监听, 注册系统的或框架的钩子来触发回调, 进行深度的功能开发

借用SQL构造器的一个例子

创建构造核心文件 of/base/sqlQuery/builder.php

<?php
/**
 * 描述 : sql请求构造器演示类
 */
class of_base_sqlQuery_builder {
    private $params = array();

    /**       描述 : 构造函数       参数 :           &data : 构造数据           &pool : 连接池      */     public function __construct(&$data, &$pool) {         $this->params['data'] = &$data;         $this->params['pool'] = &$pool;     }

    /**       描述 : 指定操作表名       参数 :            table       返回 :            当前对象      /     public function &from($table) {         $this->params['table'] = &$table;         return $this;     }

    /**       描述 : 指定操作条件       参数 :            where       返回 :            当前对象      /     public function &where($where) {         $this->params['where'] = &$where;         return $this;     }

    /**       描述 : 执行查询操作       参数 :            limit       返回 :            当前对象      /     public function &select($limit = null) {         $index = &$this->params;

        $sql = 'SELECT `' . join('`, `', $index['data']) . '`';         $sql .= ' FROM ' . $index['table'];         empty($index['where']) || $sql .= ' WHERE ' . $index['where'];         $limit === null || $sql .= ' LIMIT ' . $limit;         return of_db::sql($sql, $index['pool']);     }

    /**       描述 : 消息初始化       返回 :            参数为数组时返回构造对象, 其它返回执行结果      /     public static function &init(&$sql, &$key) {         //实例化构造对象         if (is_array($sql)) {             $result = new self($sql, $key);         //bool, null, string 直接查询         } else {             $result = &of_db::sql($sql, $key);         }

        return $result;     } }

//注册快捷方法, 重写SQL执行方式 of::link('&sql', '$sql, $key = &#039;default&#039;', 'return of_base_sqlQuery_builder::init($sql, $key);');

框架配置文件设置预加载类

<?php
//修改框架配置文件中的 preloaded 项
return array(
    //预先装载类
    'preloaded'   => array(
        //加载SQL构造器
        'of_base_sqlQuery_builder '
    )
);

调用演示

<?php
$data = L::sql(array('id', 'name'))
    ->from('_of_sso_func')
    ->where('id > 1')
    ->select('2, 3');
print_r($data);

Clone this wiki locally