SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 27 of 27
  1. #26
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps a best practice would be to always use something like:
    PHP Code:
    class Global {

    function 
    get($key) {
        return 
    $GLOBALS[$var];
    }

    function 
    set($key$value null) {
        
    $GLOBALS[$key] = $value;
    }

    function 
    has($key) {
        return isset(
    $GLOBALS[$key]);
    }


    Look familiar?
    Christopher

  2. #27
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    Perhaps a best practice would be to always use something like:
    PHP Code:
    class Global {

    function 
    get($key) {
        return 
    $GLOBALS[$var];
    }

    function 
    set($key$value null) {
        
    $GLOBALS[$key] = $value;
    }

    function 
    has($key) {
        return isset(
    $GLOBALS[$key]);
    }


    Or even:

    PHP Code:

    class Global
    {
        private static 
    $instance;
        private 
    $values;

        private function 
    __construct()
        {
        }

        public static function 
    instance()
        {
            if(empty(
    self::$instance)) {
                
    self::$instance = new self();
            }
            return 
    self::$instance;
        }

        public function 
    __get($key)
        {
            return isset(
    self::instance()->values[$key]) ? self::instance()->values[$key] : null;
        }

        public function 
    __set($key$value null)
        {
            if(
    is_null($value)) {
                if(isset(
    self::instance()->values[$key])) {
                    unset(
    self::instance()->values[$key]);
                }
            }
            else {
                
    self::instance()->values[$key] = $value;
            }
        }

        public static function 
    get($name)
        {
            return 
    self::instance()->__get($name);
        }

        public static function 
    set($name$value null)
        {
            
    self::instance()->__set($name$value);
        }


    Quote Originally Posted by arborint
    Look familiar?
    Not really...


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
  •