Search code examples
phpstdclass

PHP Access StdClass's within an athor StdClass


How to access to StdClass's element who is within an athor StdClass.

this is what var_dump return:

 stdClass Object
(
    [return] => stdClass Object
        (
            [barCode] => 530884HB|4320000007;502241VA|4320000008;
            [code] => 0
            [idAffiliate] => 0
            [idOrder] => 25
            [idProduct] => 320
            [message] => Résérvation effectué avec succés
            [quantity] => 2
        )

)

And I want to get the value of the [barCode]??


Solution

  • Since the property return is a public property, you could simply get the barCode like this:

    $object->return->barCode

    Example:

    $object = new StdClass();
    $object->return = new StdClass();
    $object->return->barCode = '530884HB|4320000007;502241VA|4320000008';
    

    var_dump of the object:

    object(stdClass)#1 (1) {
      ["return"]=>
      object(stdClass)#2 (1) {
        ["barCode"]=>
        string(39) "530884HB|4320000007;502241VA|4320000008"
      }
    }
    

    print_r of the object:

    stdClass Object
    (
        [return] => stdClass Object
            (
                [barCode] => 530884HB|4320000007;502241VA|4320000008
            )
    
    )