SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast codlib's Avatar
    Join Date
    Jan 2007
    0 Post(s)
    0 Thread(s)

    Question How will i pass an array as key to __set magic method

    I have a singleton session class as follows.

    Code PHP:
    class Session {
        static private $_instance = NULL;
        private function __construct() 
    	* Prevents the class from being cloned
    	* @return NULL
    	private function __clone() { }
    	* Returns the singleton instance of this class
    	* @return Session
        public static function getInstance() 
            if (!self::$_instance) {
                self::$_instance = new Session();
            return self::$_instance;
        public function __get($key) {
            if (isset($_SESSION[$key])) {
                return $_SESSION[$key];
            return NULL;
        public function __set($key, $value) 
            $_SESSION[$key] = $value;
    	public function __isset($key) {
            return isset($_SESSION[$key]);
        public function __unset($key) {

    I can create an object as follows

    $session = Session::getInstance();
    $session->name = 'some name';

    I can also get the value like

    echo $session->name;

    The problem is, i want to pass an array to this object and it is not working. for example, i wan to set something like
    $_SESSION['user']['data'] = array('name'=>'some name',"empId"=>'123');

    I am trying like this.

    $session->['user']['data'] = array('name'=>'some name',"empId"=>'123'); but it is not working. Could you please suggest what is wrong.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    North-East, UK.
    3 Post(s)
    0 Thread(s)
    You will need to implement the ArrayAccess interface, or possibly even extend ArrayObject which already implements ArrayAccess.
    @AnthonySterling: I'm a PHP developer, a consultant for and the organiser of @phpne, a PHP User Group covering the North-East of England.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts