bart2puck
bart2puck

Reputation: 2522

how to determine if objects children has children

I have an object defined below:

    $userAttributes->deviceInfo->macAddress->type = "text";
    $userAttributes->deviceInfo->macAddress->required = "*";
    $userAttributes->deviceInfo->macAddress->options = NULL;
    $userAttributes->deviceInfo->macAddress->size = "16";

    $userAttributes->callControl->guest->isActive->type = "select";
    $userAttributes->callControl->guest->isActive->required = "*";
    $userAttributes->callControl->guest->isActive->options = $tF;
    $userAttributes->callControl->guest->isActive->size = NULL;

if i do:

   foreach ($userAttributes as $key => $value)
   {
       foreach ($value as $k => $v)
       {
            echo $v->type;
            echo $v->required;
            echo $v->options;
            echo $v->size;
       }
   }

this is going to fail when it gets to the callControl piece. how can i determine if $v-> has children? The property "isActive" could be any number of things so i cant assume anything and do $v->isActive->Blah

how can i determine if $v-> has children and if so iterate through that?

Upvotes: 0

Views: 821

Answers (1)

gpopoteur
gpopoteur

Reputation: 1519

PHP has a built in function to check if something is an object. It's called is_object and it checks if the parameter that you pass to it is an object.

Further reading: http://php.net/manual/es/function.is-object.php

For your specific case you can create a function to call it recursively:

   function getAttributes($obj){
        foreach($obj as $k => $v){
            if(is_object($v)){
                getAttributes($v);
            }else{
                echo $v;
            }
        }
    }

Upvotes: 2

Related Questions