[PHP] PHP4 static properties - PEAR vs my solution
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 ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP4 static properties - PEAR vs my solution
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 ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP4 static properties - PEAR vs my solution
On Fri, October 6, 2006 7:22 am, Hodicska Gergely wrote: I'm curious about your opinion. class base { function staticProperty($name, $value = null) { static $properties = array(); This would be Really Nifty, if PHP allowed multiple inheritence so you could have a mixin class... Otherwise, however, you have to have a common base class across all the various class systems in use in your multitude of web applications, or duplicate the code (ugh!) in each. So if you have a single base class to throw this into, it's great. If you have a plethora of code-bases you work with, the global variable hack is probably more suitable. YMMV -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP4 static properties - PEAR vs my solution
On Fri, October 6, 2006 10:59 am, Hodicska Gergely wrote: This would be Really Nifty, if PHP allowed multiple inheritence so you could have a mixin class... Otherwise, however, you have to have a common base class across all the various class systems in use in your multitude of web applications, or duplicate the code (ugh!) in each. So if you have a single base class to throw this into, it's great. If you have a plethora of code-bases you work with, the global variable hack is probably more suitable. Maybe you missed the subject of my mail. This wouldn't be a general solution, just a better one than it is now in PEAR. And PEAR has a base class, all PEAR classes have to extend it. But users of the PEAR solution to staticProperties may or may not want any other portion of PEAR as well. So it's only better if it suits the needs of the user. You'd have to ask the PEAR users if it's better for them. Perhaps you meant to post on a PEAR list. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP4 static properties - PEAR vs my solution
But users of the PEAR solution to staticProperties may or may not want any other portion of PEAR as well. But they have the possibility to choose. ;) So it's only better if it suits the needs of the user. You'd have to ask the PEAR users if it's better for them. 1. I sent it here because I think it is an interesting solution, it could be useful for any PHP programmer who wants to know more than the surface. 2. I'm not a PEAR fan but I think that PEAR is very close to PHP. Perhaps you meant to post on a PEAR list. I sent it to the pear-dev list too, and I never thought that after a mail they will change the 4 years old code, and make a BC change. I just was curious if they choose knowingly their solution. Regards, Felhő -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php