Results 1 to 8 of 8
- 08-31-2010, 11:56 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
Java equivalent of PHP's variable-variables?
Hi to those of you familiar with PHP,
Here's some example code in PHP:
PHP Code:class MyClass { public function foo($a, $b) { //Do something with $a and $b. } public function bar($x, $y) { //Do something completely different with $x, $y. } public function fred($q, $r) { //Do something else } public function ... //etc (lots of functions). } function dynamic($method) { $mc = new MyClass; if (method_exists($mc, $method)) $mc->$method("apple", "pear"); else echo "Oops!\n"; }
So, I'd like to be able to do this in Java without a huge if block that tediously goes through: if method == "foo" then call foo, else if method == "bar" then call bar, etc, etc ... *yawn*!
Can someone tell me how to do this please?
Many thanks in advance.Last edited by XmisterIS; 08-31-2010 at 12:01 PM.
- 08-31-2010, 12:01 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Google "reflection".
- 08-31-2010, 01:47 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
And before doing that ensure that this is what you want to do, and you're not simply using your PHP knowledge and attempting to convert what you would do in PHP directly into Java.
- 08-31-2010, 02:09 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I was hoping that, after investigating reflection and reading the warnings and such contained in the API docs and tutorials, that the OP would realise, himself, how inherently "dangerous" that sort of thing is and decide that its not really what he wanted and that he should solve his "problem" in an OO manner rather than a "scripting" manner. But, more likely, he'd just whine and complain about why it is so "hard" to do in Java.
- 08-31-2010, 02:20 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I'd have thought you'd have had that hope crushed out of you by now...
- 08-31-2010, 05:20 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
- 08-31-2010, 05:24 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Like I said "more likely", as that is what 99% percent of all people do who come and say "how do I do 'x' as I do in 'y'". Considering that it is "more likely".
- 09-01-2010, 11:05 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Java equivalent of Linux 'find' function
By porchrat in forum New To JavaReplies: 5Last Post: 08-25-2010, 11:31 AM -
[SOLVED] Java equivalent to X11 Work Procedure
By LongHitter in forum New To JavaReplies: 0Last Post: 01-12-2009, 11:36 AM -
Filter a file in java (unix cut command equivalent)
By marcosabel in forum New To JavaReplies: 0Last Post: 02-11-2008, 07:26 PM -
Php explode function, equivalent java
By fernando in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 05:44 AM -
building file and variable names from variables
By madad2005 in forum New To JavaReplies: 2Last Post: 07-18-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks