> but will be glad to discuss the merits of this and
> its implementation if any one else feels it could be a useful feature.

Hi Derrell,

Below is some code I wrote to do something very similar to this from
an external perspective. It basically replaces the use of $this with a
reference to the class's own getInstance() method. The getInstance is
not used in code which calls the class, so the fact that it is all
running off the same instance is not visible from outside.

I'm not sure what the memory usage looks like if you do things like
this. I think the key would be to put __constructor code in the
getInstance mathod instead. A clean way to do this internally would be
nice. A way to redefine $this may be better than a __new() method.
Some sort of method_preview would be good enough for the code belown
(where $this would be set to self::instance()).

class StaticClass {
        
        //// Statics Code ////
        
        static protected $instance = false;
        
        protected function instance() { 
                if(!self::$instance) { 
                        self::$instance = new self; 
                } 
                return self::$instance; 
        }

        //// Class Code ////
        
        protected $value;
        
        function set_value($value) {
        $t = self::instance(); // Call this whenever you want a private variable
        
                $t->value = $value;
        }
        
        function get_value() {
        $t = self::instance();
        
                return $t->value;
        }
}

Regards,
Douglas

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to