2

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)
  }
}
13
  • Perhaps stackoverflow.com/questions/27929875/…? Commented Sep 26, 2019 at 18:53
  • 1
    So we can close with Nigel's link then? Commented Sep 26, 2019 at 18:56
  • 1
    @CodeJunkie LOL! I know huh? I even tried it myself once, and twice... just to see if things changed; alas... they haven't :D Commented Sep 26, 2019 at 19:01
  • 1
    @NigelRen Well it's all the same to me :) If you want, I can leave it open and have you post an answer. Or, let me know now and I can use the link you posted above as a duplicate. I'm in no hurry ;-) nor eager lol Commented Sep 26, 2019 at 19:07
  • 1
    @FunkFortyNiner It worked :D -- in 2 days time I will accept my own answer hahaha Commented Sep 26, 2019 at 19:31

3 Answers 3

5

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');
Sign up to request clarification or add additional context in comments.

Comments

3

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){
}

`

Comments

3

you can change it to this :

$str1 = 'wind';
$str2 = 'windii';
echo $testObj->{$str1}->{$str2};

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.