Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP __call equivalent for java

Tags:

java

php

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.

From the PHP manual on magic methods:

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

like image 444
Thizzer Avatar asked Jan 12 '10 16:01

Thizzer


People also ask

What is__ invoke in PHP?

The __invoke method is a way that PHP can accommodate pseudo-first-class functions. The __invoke method can be used to pass a class that can act as a closure or a continuation, or simply as a function that you can pass around. A lot of functional programming relies on first class functions.

What is the magic method in PHP?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

What is magic method?

Magic methods are special methods in python that have double underscores (dunder) on both sides of the method name. Magic methods are predominantly used for operator overloading.


2 Answers

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.

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.

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.

like image 56
Tendayi Mawushe Avatar answered Oct 22 '22 02:10

Tendayi Mawushe


No, there is not.

like image 24
Kevin Avatar answered Oct 22 '22 01:10

Kevin