【发布时间】:2020-10-18 07:13:27
【问题描述】:
寻找一种灵活的方式来允许其他开发人员扩展模板系统的渲染方法,基本上允许他们生成自己的 render::whatever([ 'params' ]) 方法。
从单个开发人员的角度来看,当前的设置运行良好,我根据上下文(帖子、媒体、分类等)设置了许多类,使用 __callStatic 方法收集检查的调用函数如果 method_exists 在类中,如果是,则提取任何传递的参数并呈现输出。
快速示例(伪代码):
-- 查看/page.php
render::title('<div>{{ title }}</div>');
-- app/render.php
class render {
public static function __callStatic( $function, $args ) {
// check if method exists
if ( method_exists( __CLASS__, $function ){
self::{ $function }( $args );
}
}
public static function title( $args ) {
// do something with the passed args...
}
}
我希望允许开发人员从他们自己包含的类中扩展可用的方法 - 这样他们就可以创建例如 render::date( $args ); 并将其传递给他们的逻辑以收集数据,然后再将结果呈现给模板。
问题是,哪种方法最有效且性能最佳 - 错误是安全性目前不是一个大问题,可能会在以后出现。
编辑 --
我已经通过执行以下操作(再次伪代码..)来完成这项工作:
-- app/render.php
class render {
public static function __callStatic( $function, $args ) {
// check if method exists
if (
method_exists( __CLASS__, $function
){
self::{ $function }( $args );
}
// check if method exists in extended class
if (
method_exists( __CLASS__.'_extend', $function
){
__CLASS__.'_extend'::{ $function }( $args );
}
}
public static function title( $args ) {
// do something with the passed args...
}
}
-- child_app/render_extend.php
class render_extend {
public static function date( $args = null ) {
// do some dating..
}
}
这里的问题是这仅限于基础 render() 类的一个扩展。
【问题讨论】:
标签: php class namespaces extend