Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 943601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:27:42+00:00 2026-05-15T22:27:42+00:00

Because I noticed it was a pattern I was constantly re-implementing, I made a

  • 0

Because I noticed it was a pattern I was constantly re-implementing, I made a DataContainer class that overrides __set,__get,__isset,__unset, and implements ArrayAccess, Countable, IteratorAggregate, and Serializable

For example, my View class, which renders PHP templates in an MVC fashion, inherits from DataContainer so that it has overloaded access to the data that gets supplied to the template.

Now, I am finding myself wanting to create a Session class to abstract away from PHP’s low-level session handling. It occured to me that this Session class would do just about everything DataContainer does, and in fact, is-a DataContainer – it holds data.

However, if I inherit from DataContainer, then all the overloaded calls go to its private $_data array. Of course, I can override DataContainer’s public get($key), public set($key,$val), etc methods, but the only thing I would be doing is renaming $this->_data to $_SESSION.

Is it possible to set a variable as a reference to a special global like $_SESSION?

class Session extends DataContainer {
    //singleton stuff
    private function __construct() {
        $this->_data =& $_SESSION;
    }
}

Is it even a good idea to do this? If not, what do you suggest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:27:42+00:00Added an answer on May 15, 2026 at 10:27 pm

    Is it possible to set a variable as a
    reference to a special global like
    $_SESSION?

    Yes, $this->_data =& $_SESSION;

    Is it even a good idea to do this?

    I don’t see why not, one could argue that it may be better to pass the data in to the constructor by reference so that it can be used for any array not just session.

    Thus, Yes.

    edit: as a side point, remember you don’t always have a session, sometimes your running on cli etc, personally I have my own session object (just a DataContainer like yours) which I then persist to $_SESSION where needed, or file or.. – ie I save (stateful) session objects in the $_SESSION rather than use the $_SESSION as the session data, if that makes sense..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that my program was crashing because it was running out of memory.
I ask because I noticed that many 64 bit EXEs link against what appear
programming my arduino microcontroller board in C, I noticed a strange behaviour. Because of
Because I've made .cpp files and then transferred them into .h files, the only
Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++()
I've noticed while on my quest to lean functional programming that there are cases
I'm currently reading Oreilly's Learning XNA 4.0 and I've noticed that the author is
In the years that I've been at my place of employment, I've noticed a
I've just started working at a new company and noticed something that looks completely
I'm working on a PHP project with solid unit-tests coverage. I've noticed, that last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.