PHP的__call相当于java [英] PHP __call equivalent for java

查看:116
本文介绍了PHP的__call相当于java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

__call of PHP 是否有Java等价物?

Is there a Java equivalent for the __call of PHP?

如果情况并非如此,那对我来说是有道理的,因为它可能会导致编译错误。

It would make sense to me if that's not the case, because it would probably result in compiler errors.

来自魔术方法的PHP手册


__ call()在调用对象中不可访问的方法时触发上下文。

__call() is triggered when invoking inaccessible methods in an object context.


推荐答案

这种动态方法/属性解析在动态类型语言中很常见,例如Java语言不直接支持PHP,Python和Ruby。

This sort of dynamic method/attribute resolution which is common in dynamically typed languages such as PHP, Python and Ruby is not directly supported in the Java language.

可以使用动态代理,要求您拥有一个动态解析实现的接口。第三方库(例如 CGLIB )允许使用普通Java类进行类似的操作。

The effect can be approximated using Dynamic Proxies which requires you to have an interface for which the implementation will be dynamically resolved. Third party libraries such as CGLIB allow similar things to be done with normal Java classes.

这种基于API的特殊情况下的方法调用拦截方法不如直接的,在PHP中可以通过 __ call 获得的支持。其他动态类型语言中的等效功能(例如Python中的 __ getattr __ )。这种差异是由于在两种语言中处理方法调度的根本不同方式。

This API based, special case interception of method invocation is not as convenient as the direct, always on support you can get with __call in PHP or equivalent features in other dynamically typed languages (such as __getattr__ in Python). This difference is due the fundamentally different ways in which method dispatch is handled in the two types of languages.

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

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