PHP的装饰等效? [英] PHP equivalent for a python decorator?

查看:102
本文介绍了PHP的装饰等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过另一个函数来包装一个PHP函数,但保留它的原始名称/参数列表。

例如:

 函数A(){
printinside A()\\\
;
}

函数Wrap_A(){
print调用A()\ n;
A();
printFinished calling A()\\\
;
}

//< ---在这里做一些魔术(实际上是A = Wrap_A)

A();

输出:

 在A()中调用A()

完成调用A()


runkit 可能 help you



另外,你总是可以用OO的方式做到这一点。把原来的乐趣放在一个类中,并将装饰器放入扩展类中。实例化并去。


I want to be able to wrap a PHP function by another function, but leaving its original name/parameter list intact.

For instance:

function A() {
    print "inside A()\n";
}

function Wrap_A() {
    print "Calling A()\n";
    A();
    print "Finished calling A()\n";
}

// <--- Do some magic here (effectively "A = Wrap_A")

A();

Output:

Calling A()
inside A()
Finished calling A()

解决方案

Apparently runkit might help you.

Also, you can always do this the OO way. Put the original fun in a class, and the decorator into an extended class. Instantiate and go.

这篇关于PHP的装饰等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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