Multiple isset block going from more specific to more generic I have a data structure that allows for inheritance of properties, in such a way that if a property is not defined within a context, the second most specific context is checked until said property is found. Simplified it's basically the following:

function getCarPartColor($carData, $owner, $brand, $part) {
    $color = false;

    if(isset($carData-&gt;{$owner}-&gt;{$brand}-&gt;{$part})) {
        $color = $carData-&gt;{$owner}-&gt;{$brand}-&gt;{$part};

    } else if(isset($carData-&gt;{$owner}-&gt;{$brand}-&gt;allparts)) {
        $color = $carData-&gt;{$owner}-&gt;{$brand}-&gt;allparts;

    } else if(isset($carData-&gt;{$owner}-&gt;allbrands-&gt;allparts)) {
        $color = $carData-&gt;{$owner}-&gt;allbrands-&gt;allparts;

    } else if(isset($carData-&gt;allowners-&gt;allbrands-&gt;allparts)) {
        $color = $carData-&gt;allowners-&gt;allbrands-&gt;allparts;
    }

    return $color;
}


Where $carData is the data structure and the rest are the querying variables. If I were to call:

getCarPartColor($carData, "AnthonyNewel", "opel", "engine");


But I hadn't defined any color for $carData-&gt;AnthonyNewel-&gt;opel-&gt;engine, then it checks if a color is set on $caraData-&gt;AnthonyNewel-&gt;opel-&gt;allparts, and fetches this one instead if it exists. If it's not there, it then checks if it's defined for allbrands, and it goes on.

While the code above works, one can't help but notice the repetition. Is there a less verbose way to write this while still being maintanable (i.e. not a long ternary operator)?
Long time reader, first time poster, welcoming of critique.

You could create an array populated with each variant of the property you're looking for:

    $is_set = array (
        $carData-&gt;{$owner}-&gt;{$brand}-&gt;{$part},
        $carData-&gt;{$owner}-&gt;{$brand}-&gt;allparts,
        $carData-&gt;{$owner}-&gt;allbrands-&gt;allparts,
        $carData-&gt;allowners-&gt;allbrands-&gt;allparts
    );


And then check each value in the array using a foreach loop which ends when the condition isset evaluates to true. The first property to evaluate to true is then returned which in turn breaks you out of the function (credit to Brett for the insight).

    foreach ($is_set as $this_set) {
        if isset($this_set) {
            return $this_set;
        }
    }


Putting it all together would look like:

function getCarPartColor($carData, $owner, $brand, $part) {

    $is_set = array (
        $carData-&gt;{$owner}-&gt;{$brand}-&gt;{$part},
        $carData-&gt;{$owner}-&gt;{$brand}-&gt;allparts,
        $carData-&gt;{$owner}-&gt;allbrands-&gt;allparts,
        $carData-&gt;allowners-&gt;allbrands-&gt;allparts
    );

    foreach ($is_set as $this_set) {
        if isset($this_set) {
            return $this_set;
        }
    }
}