SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    London, England
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A kind of singleton array?

    I was thinking of doing something like below, a multiple singleton kinda thing
    to store user data to save using joins everytime I need to add user data to
    something. I would use multiple users data in multiple places in a single
    request. Was hoping for it to act like kinda a cache. Would it work, is there a
    better way?

    PHP Code:
    <?php

    class User
    {
        static private 
    $instances = array();

        static public function 
    getUserID($userID)
        {
            if (empty(
    self::$instances[$userID]))
            {
                
    self::$instances[$userID] = new self($userID);
            }

            return 
    self::$instances[$userID]; 
        }

        private function 
    __construct($userID)
        {
            
    // get user info
        
    }
    }

    $user User::getUserID(4);
    $user->setSomeStuff("foo");

    $user User::getUserID(7);
    $user->setSomeStuff("bar");

    $user User::getUserID(4);
    echo 
    $user->getSomeStuff(); // foo

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you invalidate the "cache" when setSomeStuff is called..?


Bookmarks

Posting Permissions

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