1

Okay, Lets say I have an object with properties like this:

$testObj->wind->windi // this returns a value like 100 mph
$testObj->wind->windii // this returns a value like 110 mph

I need to access a property but I don't know what property explicitly, Instead I have a string like this:

$str = 'wind->windii';

Calling predefined values works:

echo $testObj->wind->windii; //results in 110 mph

The Problem: I need a way to get the values without hard coding the actual properties:

echo $testObj->$str; //and have it result in 110 mph

I thought this was a variable variables situation but I had no luck getting that to work and after a few hours of frustration I'm asking for help

*edit: I also need to mention that the object name changes as well so we can't hard code $testObj but rather be able to take any object and any string and get it's properties

Also, bracket notation isn't working, it results in "Undefined property: stdClass::$wind->elevation"

For those following at home, this is the var_dump of $testObj

object(stdClass)#299 (2) {
  ["snow"]=>
  object(stdClass)#315 (3) {
    ["elevation"]=>
    string(5) "365.4"
    ["results"]=>
    string(1) "6"
    ["status"]=>
    int(1)
  }
  ["wind"]=>
  object(stdClass)#314 (5) {
    ["elevation"]=>
    string(5) "365.4"
    ["windi"]=>
    string(7) "100 mph"
    ["windii"]=>
    string(7) "110 mph"
    ["windiii"]=>
    string(7) "115 mph"
    ["status"]=>
    int(1)
  }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
CodeJunkie
  • 339
  • 3
  • 16

3 Answers3

3

you can change it to this :

$str1 = 'wind';
$str2 = 'windii';
echo $testObj->{$str1}->{$str2};
Mosi
  • 198
  • 6
  • No, So the problem is that the string is unknown, not to mention the dimensions of the properties, so I can't hard code individual strings since I have no idea how many I need yet. – CodeJunkie Sep 26 '19 at 19:16
  • you said I have a string like it : { $str = 'wind->windii' }. yes or no? if you have that variable, you can split it with "->" separator and now you have an array of properties – Mosi Sep 26 '19 at 19:22
  • To make your answer work I'd have to concatenate ->$str# to $testObj and then evaluate the entire string.... Which is exactly the problem but now we've added $testObj to the beginning of the string. Thanks for your input though – CodeJunkie Sep 26 '19 at 19:37
3

Finally I have working code.
With mental support from @NigelRen
Emotional support from @FunkFortyNiner
And most of the code from this question about arrays

Test Object:

$obj = json_decode('{"snow":{"elevation":"365.4","results":"6","status":1},"wind":{"elevation":"365.4","windi":"100 mph","windii":"110 mph","windiii":"115 mph","status":1}}');

Test Directory:

$path = 'wind:windii';

Getter:

  function get($path, $obj) {
    $path = explode(':', $path);
    $temp =& $obj;

    foreach($path as $key) {
      $temp =& $temp->{$key};
    }
    return $temp;
  }
  var_dump(get($path, $obj)); //dump to see the result

Setter:

  function set($path, &$obj, $value=null) {
    $path = explode(':', $path);
    $temp =& $obj;

    foreach($path as $key) {
      $temp =& $temp->{$key};
    }

    $temp = $value;
  }
  //Tested with:
  set($path, $obj, '111');
CodeJunkie
  • 339
  • 3
  • 16
2

In PHP you can access properties dynamically such as:

`

class test {
    public testvar;
}
$newtest = new test();
$property = "testvar"
$newtest->{"testvar"} //or $newtest->{$property};

`

Depending on what you are doing you can you a foreach loop to get the key value pair as well.

`

foreach($newtest as $key=>$val){
}

`

leroyjenkinss24
  • 472
  • 3
  • 8