Results 1 to 8 of 8
  1. #1
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default 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.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Google "reflection".

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    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.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    I'd have thought you'd have had that hope crushed out of you by now...

  6. #6
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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.
    Actually, I found the documentation to be very enlightening and interesting reading. Thank you for your original reply ... but please don't make assumptions about me without knowing anything about me! Many thanks.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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".

  8. #8
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    I love you too :p

    I've ended up not needing reflection, I figured out how to do it with objects. :)

Similar Threads

  1. Java equivalent of Linux 'find' function
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 08-25-2010, 11:31 AM
  2. [SOLVED] Java equivalent to X11 Work Procedure
    By LongHitter in forum New To Java
    Replies: 0
    Last Post: 01-12-2009, 11:36 AM
  3. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 07:26 PM
  4. Php explode function, equivalent java
    By fernando in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:44 AM
  5. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •