WEB PHP: Variable in Object notation

Discussion in 'OT Technology' started by fishbulb, Apr 9, 2010.

  1. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    I doubt there's a way to accomplish this, but ..

    With arrays, you can use variable names for indexes, as in
    $myArray[$myIndex];

    Is there a special syntax to get the same to work with Objects, or can it not be done?
    so instead of $myObject->property3;
    I want something like $myObject->$dynamicProperty
    ?
    I tried it as noted, but it threw an error..
     
  2. imraven52

    imraven52 New Member

    Joined:
    Dec 5, 2006
    Messages:
    1,353
    Likes Received:
    0
    Location:
    salt lake city, utah
    I'm not sure what you're trying to do. Is it along these lines:

    Code:
    class test {
            public $var1;
            public $var2;
    
            function __construct() {
                    $this->var1 = 'hi';
                    $this->var2 = 'oterhihi';
            }
    }
    
    $test = new test();
    
    $var = 'var1';
    echo $test->$var."\n";
    
    $var = 'var2';
    echo $test->$var."\n";
     

Share This Page