0

I have two stdClass Object's that have a number of fields. I cannot change the object layout or structure.

For example, they both have a phone number:

Object A

[phone] => Array
(
[0] => stdClass Object
(
[value] => '+1 123 456 7890'
[primary] => 1
)
)

Object B

[phone] => '+1 123 456 7890'

I want to dynamically access them with a database entry. In other words, to access 'phone' in Object B, it is simply:

$objB->phone

And in the database, I would store the field as 'phone', assign this value to a variable e.g. $field and the code would be:

$objB->{"$field"}

The issue arises however if I want to map Object A. To access the phone value, I would need to:

$objA->phone[0]->value

But as I have many fields and many mappings, how do I dynamically set up access to these fields?

Assuming I had a database entry like:

phone.0.value

This could be easily translated to:

$objA->{"$field1"}[$key]->$field2

But what happens in a dynamic or nested case, for example, another field is:

email.company.0.value.0

I'm not aware of a way to access this value dynamically. In other words, how can I build access on the fly for a given database entry?

$objA->{"$field1"}->{"$field2"}[$key1]->$field3[$key2]
Colin
  • 675
  • 1
  • 11
  • 32
  • 1
    I asked this quesiton once on stack overflow. The answer was next to nothing. Basically you can have your app either object driven or table driven but not both (at least not cleanly). If you do not sort/filter/join you can use a object style database. If you want to use sort/filter/join you should use associative array instead of object. That's the best answer I could get. – danielson317 Apr 09 '20 at 15:08
  • Interesting. Do you have a link to your question by any chance? Thanks! – Colin Apr 09 '20 at 15:30
  • I looked after posting that comment but could not find it. I'll take another stab. It may have been deleted as duplicate or something. – danielson317 Apr 09 '20 at 15:32
  • Still can't find the question but found this interesting bit: https://stackoverflow.com/questions/17421306/loading-a-php-class-object-from-a-database-best-practices-database-handle-etc – danielson317 Apr 09 '20 at 15:37
  • I edited my Answer. please check it. – Mohammad Zare Moghadam Apr 09 '20 at 16:19

1 Answers1

1

You can create a function for doing this:

function test($str, $c) {
    $res = $c;
    $array = explode('.', $str);
    foreach ($array as $item) {
        if (is_numeric($item)) {
            $res = $res[$item];
        } else {
            $res = $res->$item;
        }
    }
    return $res;
}
// $c is your \StdClass
echo test('email.company.0.value.0', $c);
  • Thanks but this doesn't answer the question. Your code assumes that I know in advance how deep/nested I need to go and ignores the fact that I may have arrays. The issue isn't access, I know how to do that, it's how I access dynamically. – Colin Apr 09 '20 at 15:30
  • 1
    Thank, I reviewed this and it appears to work as I would like it to. Very much appreciate the edit! :) – Colin Apr 14 '20 at 12:21