Jump to content

How to Reference the Property of an Object that Is A Property Of an Object


danallen

Recommended Posts

Hi, sometimes simple things are hard.  I asked at the pub down the street, but no one had a clue.

 

How do we reference the property [sess_expiration] in the object below?  Seems like it should be possible to use

$this->config->config->sess_expiration

Array(
    [config] => Config Object        (            [config] => Array                (                    [sess_cookie_name] => weird_session                    [sess_expiration] => 2592000                )        )

)

 

This does the job, but this is stupid-looking:

$value_i_need=get_object_vars(get_object_vars($this)['config'])['config']['sess_expiration']

 

If you could fill me in on how to deal with this, I would really appreciate it.

 

Thank you.

 

Link to comment
Share on other sites

Looks like you did a

print_r(get_object_vars($this))
Right?

 

1. The thing you printed is an array, because that's what get_object_vars does. But $this is an object.

2. "config" is the thing inside it.

3. It is an object (a Config object).

4. "config" is (again) the next thing inside it.

5. It is an array.

6. "sess_expiration" is one of the values you want.

 

Objects use -> and arrays use [].

$this->config->config["sess_expiration"]
Link to comment
Share on other sites

  • 1 year later...

It's been awhile since posting this, but I still appreciate the response. Usually, I am working with arrays. I have had occasion to catch up to a degree on php objects, how and why they are used, read some discussions of trade-offs, and after all that, I would have to say, your explanation is exactly perfect from my perspective, so thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.