PHP:将一个类的所有函数包装在一个子类中 [英] PHP: Wrap all functions of a class in a subclass

查看:34
本文介绍了PHP:将一个类的所有函数包装在一个子类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHP 库类,我想将它的所有公共函数包装在一个子类中......大致如下:

class BaseClass{函数 do_something(){一些;东西;}函数 do_something_else(){其他;东西;}/** 这里有 20 个左右的其他功能!*/}类 SubClass 扩展 BaseClass{函数magicOverrideEveryone(){之前要做的事情;//即显示标​​题call_original_function();//即显示其他未修饰的内容之后要做的事情;//即显示页脚}}

归根结底,如果有一种[有点优雅/干净]的方法可以在一个地方完成所有工作,我宁愿不必用相同的包装器代码覆盖每个超类方法.

这可能吗?我怀疑我在这里的元编程领域,甚至不知道 PHP 是否提供这样的野兽,但我想我会问......

解决方案

您可以使用 __call 魔法方法和不直接从基类继承的通用代理"类.

这是一个代理类的(接近)完整实现,它包装了您传递给它的任何对象.它将围绕每个方法调用调用一些之前"和之后"代码.

class MyProxy {函数 __construct($object) {$this->object = $object;}函数 __call($method, $args) {//在代码之前运行//在我们的代理对象上调用原始方法call_user_func_array(array($this->object, $method), $args);//在此处执行代码}}$base = new BaseClass();$proxy = new MyProxy($base);$proxy->doSomething();//调用 $base->doSomething();

您当然希望添加一些错误处理,例如询问代理对象是否响应 __call 中的给定方法,如果没有则引发错误.您甚至可以将 Proxy 类设计为其他代理的基类.子代理类可以实现beforeafter 方法.

缺点是您的子类"不再实现 BaseClass,这意味着如果您使用的是 type-hinting 并且希望仅将 BaseClass 类型的对象传递给函数,这种方法将失败.>

Working with a PHP library class, and I'd like to wrap all of its public functions in a subclass... Something along the lines of:

class BaseClass
{
   function do_something()
   {
        some;
        stuff;
   }

   function do_something_else()
   {
        other;
        stuff;
   }

   /*
    * 20-or-so other functions here!
    */
}

class SubClass extends BaseClass
{
   function magicalOverrideEveryone()
   {
        stuff-to-do-before;        // i.e. Display header
        call_original_function();  // i.e. Display otherwise-undecorated content
        stuff-to-do-after;         // i.e. Display footer
   }
}

Boiling it down, I'd prefer not to have to override every superclass method with the same wrapper code, if there's a [somewhat elegant / clean] way to do it all in one place.

Is this possible? I suspect I'm in metaprogramming land here, and don't even know if PHP offers such a beast, but figured I'd ask...

解决方案

You could do this easily with the __call magic method and a generic "proxy" class which doesn't inherit directly from the base class.

Here is a (near) complete implementation of a proxying class which wraps whatever object you pass it. It will invoke some "before" and "after" code around each method call.

class MyProxy {
  function __construct($object) {
    $this->object = $object;
  }

  function __call($method, $args) {
    // Run before code here

    // Invoke original method on our proxied object
    call_user_func_array(array($this->object, $method), $args);

    // Run after code here
  }
}


$base = new BaseClass();
$proxy = new MyProxy($base);

$proxy->doSomething(); // invoke $base->doSomething();

You would of course want to add a bit of error handling, like asking the proxied object if it responds to the given method in __call and raising an error if it doesn't. You could even design the Proxy class to be a base-class for other proxies. The child proxy classes could implement before and after methods.

The downside is that your "child class" no longer implements BaseClass, meaning if you're using type-hinting and want to demand that only objects of type BaseClass are passed into a function, this approach will fail.

这篇关于PHP:将一个类的所有函数包装在一个子类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆