Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从类';他自己的方法_Php_Oop_Inheritance_Refactoring - Fatal编程技术网

Php 从类';他自己的方法

Php 从类';他自己的方法,php,oop,inheritance,refactoring,Php,Oop,Inheritance,Refactoring,我参与了一个项目,其中我有一个扩展ADODB_基类的数据库类-我的目标是用一个特殊的数据库包装层类替换ADODB_类 该类有约200个方法;识别它从ADODB_类继承的方法(我必须在自定义包装器中使用相同的名称/参数重新创建这些方法,以避免重构过程中出现大的头缓存)和它自己的方法,有什么好的做法 编辑:示例代码: class Postgres extends ADODB_base{ //[...] // This method exist in Postgres class bu

我参与了一个项目,其中我有一个扩展ADODB_基类的数据库类-我的目标是用一个特殊的数据库包装层类替换ADODB_类

该类有约200个方法;识别它从ADODB_类继承的方法(我必须在自定义包装器中使用相同的名称/参数重新创建这些方法,以避免重构过程中出现大的头缓存)和它自己的方法,有什么好的做法

编辑:示例代码:

class Postgres extends ADODB_base{
    //[...]
    // This method exist in Postgres class but not in ADODB_base,
    // let's say this is a Postgres own method.
    public function do_something()
    {
        //[...]
        // query() method belongs to ADODB_base class, so i need to create it
        // in my wrapper too, keeping the name and the args.
        $this->query([...]);
    }
    //[...]
}
我的目标是找到最佳实践,告诉人们
dou_something()
属于
Postgres
,而
query()
属于
ADODB_base
而不改变使用
Postgres
类的所有代码。


识别甚至是属性都会很好。

以下是一些方法,我不想随便说出最佳实践:

示例类 反射API几乎可以识别类的每个方面。但是,由于提供此信息需要大量的分析过程,因此这会对性能产生影响。更快的替代方案是类/对象函数。然而,可能收集的信息比反射提供的信息要有限得多

用于获取定义了方法的类 用于检查父级中是否存在方法 使用类/对象函数列出所有父方法 以上所有内容的现场演示:


使用装饰器 如果这是针对某种装饰器模式的,您还可以使用magic
\u call
方法拦截对装饰器之外的方法的任何调用,并将其委托给装饰实例,例如

class Decorator 
{    
    public function __construct($instance)
    {
        $this->decoratedInstance = $instance;
    }
    public function __call($method, $args)
    {
        return call_user_func_array(
            array($this->decoratedInstance, $method), 
            $args
        );    
    }
}
请注意,所有的魔术方法都会对应用程序的性能产生严重影响,您需要对解决方案进行基准测试,以确定它在您的设置中是否可以接受


进一步阅读
关于更多的想法,请看一下other

我仍然不明白该包装器将要做什么,或者为什么它必须有ADODB_Base方法,而Postgres类将以任何方式提供这些方法,因为它继承自它。因为我必须完全删除ADODB类。在这种情况下,不能使用decorator,因为修饰类的代码将由作者删除。@OZ OP并不清楚他真正想要实现什么,所以我不排除修饰者。Gordon,他只想从代码中删除一个类。但是这个类是另一个类的“父类”,所以他必须编写替换项。@OZ那么他需要知道这个类是否属于ADODB做什么呢?他说的包装纸是什么?如果这是一种可以绑定到多个db后端的适配器,那么为它提供与ADODB相同的方法是没有意义的,因为它不会是一个抽象。
$reflector = new ReflectionMethod('B', 'fn');
echo $reflector->getDeclaringClass()->getName(); // A
$reflector = new ReflectionMethod('B', 'foo');
echo $reflector->getDeclaringClass()->getName(); // B
var_dump(method_exists(get_parent_class('B'), 'fn'));  // TRUE
var_dump(method_exists(get_parent_class('B'), 'foo')); // FALSE
print_r(get_class_methods(get_parent_class('B')));
class Decorator 
{    
    public function __construct($instance)
    {
        $this->decoratedInstance = $instance;
    }
    public function __call($method, $args)
    {
        return call_user_func_array(
            array($this->decoratedInstance, $method), 
            $args
        );    
    }
}