PHP access data of an object

后端 未结 4 2034
野性不改
野性不改 2021-01-20 14:51

I have an object of which I am looking to get a piece of data from, the object looks like this,

    Product Object
(
    [name] => Simon Test Cup
    [co         


        
相关标签:
4条回答
  • 2021-01-20 15:11

    As you might see, your Product object has an attribute range_id:

    Product Object
    (
        [name] => Simon Test Cup
        [code] => 123456789
        [category_id] => 3
        [range_id] => 26          // <--- here!
        [price] => 10.00
        ...
    

    So you should be able to get the ID via:

    $object->range_id
    

    But probably, the Product and Range classes define methods that allow you to access those information and you should use them instead. You just have to read the documentation how to use them.

    0 讨论(0)
  • 2021-01-20 15:14

    Assuming Felix' answer doesn't work (which it may very well), I'm guessing that the _related field is protected. In that case, there should be an accessor method in that class to let you get related objects. Please use get_class_methods() on the object and edit your post with the methods that are available.

    0 讨论(0)
  • 2021-01-20 15:16

    You could say $object->_related["_related"]["range"], but this structure is presumably that of an object which has accessors for things you might want from it. You're clearly not intended to worry about the internal structure of the thing.

    0 讨论(0)
  • 2021-01-20 15:17
    $product->_related["_related"]["range"]->key
    

    But i'm not sure that i've understood well

    0 讨论(0)
提交回复
热议问题