Re: PHP4 static properties - PEAR vs my solution

From: Date: Fri, 06 Oct 2006 14:42:17 +0000
Subject: Re: PHP4 static properties - PEAR vs my solution
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-44414@lists.php.net to get a copy of this message
On 10/6/06, Hodicska Gergely <felho@avalon.aut.bme.hu> wrote:
Hi! I had a little discussion on a forum topic about static class method and properties. Somebody there pointed to the PEAR::getStaticProperty() solution to emulate static properties in PHP4. I was not familiar with it but its approach seems a little strange for me. It is not really more than storing static properties in a global array. I tried to make something better and come out with this solutions, which I think is much more closer to the philosophy of a static property and maybe is more elegant (of course this is subjective :)). I'm curious about your opinion. Regards, Felhő ------------------------------------------------------------------------- <?php
        class base
        {
                function &staticProperty($name, $value = null)
                {
                        static $properties = array();
                        if (func_num_args() == 2) {
                                $properties[$name] = $value;
                                return $properties[$name];
                        } else {
                                if (array_key_exists($name, $properties)) {
                                        return $properties[$name];
                                } else {
                                        $php4_4suck = null;
                                        return $php4_4suck;
                                }
                        }
                }
        }
        class foo extends base
        {
        }
        class bar extends base
        {
        }
        foo::staticProperty('foo', 'foo');
        var_dump(bar::staticProperty('foo')); // NULL
        bar::staticProperty('foo', 'bar');
        var_dump(bar::staticProperty('foo')); // "bar"
        $foo = new foo();
        var_dump($foo->staticProperty('foo'));  // "foo"
        $bar = new bar();
        var_dump($bar->staticProperty('foo')); // "bar"
        $fooStaticProp =& foo::staticProperty('foo');
        $fooStaticProp = 'fooChanged';
        var_dump($foo->staticProperty('foo')); // "fooChanged"
?>
This does make some sense but we're not likely to be changing our getStaticProperty method for 2 reasons. 1) It's very established and basically deprecated now. We're aiming for PHP5 now and making changes to the PHP4 architecture is not something we should spend time one, especially when it already works. 2) This is slightly different than what PEAR implements as it requires 1) usage of the class name when setting properties (the PEAR method takes a string, so the class doesn't have to be defined) and 2) requires extending a specific base class. PEAR packages don't have to extend the PEAR class so this wouldn't always work for PEAR. Switching to this would be effectively a BC break and it's just not worth it for what we consider a deprecated language (see the recent RFC about having only PHP5 packages in the near future). -- Justin Patrin

« previous php.pear.dev (#44414) next »