1

I need to implement a function that will return the property 'name' of an object no matter how deeply it is embedded into other objects. I want to use dot notation to navigate through the hierarchy of objects.

Here is what I have so far:

#!/usr/bin/php
<?php

class Foo
{
    public $name = 'This is foo';
    public $Bar;
}

class Bar
{
    public $name = 'This is bar';
    public $Baz;
}

class Baz
{
    public $name = 'This is baz';
}

function getName(Foo $foo, string $obj): string {

    if (strpos($obj, '.')) {
        $path = explode('.', $obj);

        //There has to be a better way than having to explicitly handle each count!
        switch (count($path)) {
            case 2:
                return $foo->{$path[1]}->name;
            case 3:
                return $foo->{$path[1]}->{$path[2]}->name;
        }
    }

    return $foo->name;


}


$foo = new Foo();
$bar = new Bar();
$baz = new Baz();

$bar->Baz = $baz;
$foo->Bar = $bar;

echo getName($foo, 'Foo') . "\n";
echo getName($foo, 'Foo.Bar') . "\n";
echo getName($foo, 'Foo.Bar.Baz') . "\n";

?>

... I'm trying to find an elegant way to get rid of the switch statement and to support any number of levels ('Foo', 'Foo.Bar', 'Foo.Bar.Baz', [...], 'Foo.Bar.Baz.Boo.Far.Faz.[...]').

Replacing the '.' by '->' in $obj clearly does not work. Any thoughts?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Etienne Beaule
  • 207
  • 2
  • 10
  • Just change to object notation https://stackoverflow.com/questions/27929875/how-to-access-and-manipulate-multi-dimensional-array-by-key-names-path/27930028#27930028 `$temp =& $temp->$key;` etc... – AbraCadaver May 02 '20 at 14:09
  • [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. – ChrisGPT was on strike May 03 '20 at 00:14

1 Answers1

0

First, use a loop or recursion instead of a switch.

function getName(Foo $foo, string $path){
    $components=explode('.', $path);
    $current=$foo;
    foreach($components as $p){
        $current=$current->$p;
    }
    return $current->name;
}

I will further comment that that accessing arbitrary properties on objects(at least actual class instances, not StdClass objects) seems like a poor design. Depending on the actual goal, you should be able to structure your classes to better support this(perhaps define an interface Nameable that classes must implement, or perhaps move the name lookup into the class itself so that it is responsible for recursion). The ideal implementation is difficult to define without knowing the real use case.

akenion
  • 805
  • 1
  • 5
  • 9