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

  • Home
  • SEARCH
  • 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 6559069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:16:38+00:00 2026-05-25T13:16:38+00:00

Can anyone please explain to me why the following code does not set the

  • 0

Can anyone please explain to me why the following code does not set the values on the array as expected? $_SESSION[‘foo’] stays empty, even after assigning time() and rand(). I’ve checked, the __get accessor method is actually called when assigning the variables but they aren’t stored for one reason or another.

$test = Session::getSession('test');
$test->foo = array();
$test->foo[] = time();
$test->foo['baz'] = rand(1,9);
var_dump($_SESSION);

Using this simple Session wrapper

class Session 
{

    protected $namespace = null;

    public static function getSession($namespace) 
    {
        return new Session($namespace);
    }

    public static function destroySession($namespace) 
    {
        if(isset($_SESSION[$namespace])) {
            unset($_SESSION[$namespace]);
            return true;
        }

        return false;
    }

    private function __construct($namespace) 
    {
        $this->namespace = $namespace;

        if(!isset($_SESSION[$namespace])) {
            $_SESSION[$namespace] = null;
        }
    }

    public function &__get($name)
    {
        return (isset($_SESSION[$this->namespace][$name])) ? $_SESSION[$this->namespace][$name] : null;
    }

    public function __set($name, $value)
    {
        $_SESSION[$this->namespace][$name] = $value;
    }

}

In case it might be relevant, i’m using php 5.3.6

  • 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-25T13:16:38+00:00Added an answer on May 25, 2026 at 1:16 pm

    I ‘m not sure if this can be made to work at all.

    For one, to return by reference you should add the & operator at the call site as well. I ‘m not sure how that might be possible without screwing up the nice syntax you ‘re trying to achieve.

    Also, you cannot return expressions by reference (only variables). So this won’t work:

    public function &__get($name)
    {
        return (isset($_SESSION[$this->namespace][$name])) 
            ? $_SESSION[$this->namespace][$name] 
            : null;
    }
    

    At the very least it should be written as

    public function &__get($name)
    {
        $value = isset($_SESSION[$this->namespace][$name])
                 ? $_SESSION[$this->namespace][$name]
                 : null;
        return $value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please explain what the following code checks for? I can make no
the following code is a textmate javascript snippet, can anyone explain it please? cuz
Can anyone please explain me why would the following code print 20 instead of
can anyone please suggest a good code example of vb.net/c# code to put the
What does it mean by logical grouping of modules in assembly? Can anyone please
I'll appreciate if anyone can explain the logic behind protocol inheritance. e.g. what does
Can anyone please explain me how to discover a WCF service if you do
Hey all, can anyone please explain how I can subclass UIButton and override some
Can anyone please tell me if Certifying Authorities (CAs) are allowed to make modifications
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{

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.