Method missing in Java or PHP

I began to learn different languages โ€‹โ€‹to begin with. I found method_missing in Ruby very interesting, but could not find the same in Java and PHP. Is there something like method_missing in Java or PHP?

+7
source share
3 answers

PHP has __call($name, array $args) . This is a catchall that handles situations where you call a method that is not specific to the instance.

In PHP> = 5.3, there is also __callStatic($name, array $args) , which acts basically the same way only at the class level (duh).

 class MyClass { public function __call($name, array $args) { echo "You tried to call $name(".implode(',',$args)."). Silly user."; } } $k = new MyClass(); $k->doSomething(1,2,3); // You tried to call doSomething(1,2,3). Silly user. 

The Java equivalent is a little more cumbersome, and it includes something called the Proxy class. The tutorial can be found here - examples are a little generalized here.

+8
source

In Java, you can do something with interface, proxy, and reflection.

+1
source

In PHP, you can use the magic __call() method.

+1
source

All Articles