Re: OOP Static Functions vs. Objects
| From: | Jochem Maas | Date: | Sun, 30 Apr 2006 15:10:52 +0000 |
| Subject: | Re: OOP Static Functions vs. Objects | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-235150@lists.php.net to get a copy of this message | ||
Edward Vermillion wrote:
I'm still trying to get my head around all the OOP stuff and was wondering if there is any basic difference between calling a static function as opposed to creating an object, in situations where both methods will do the same thing for you. Is there any overhead to creating an object from a class that might impact time/memory consumption(efficiency), or does PHP treat these two methods the same? I'm currently working with PHP4 but am also curious as to how it works in PHP5.creating an object is a lot of overhead, after the object is created the difference in calling the method on the object and calling the other method statically is neglegible/non-existant. don't even try to compare objects in php4 and php5 on anything other than a superficial level - php5 has so much more OO functionality that it's jsut not funny. :-P for instance all those '&'s you are using - php5 doesn't need it (throws E_STRICT errors at you actually if you use them) because objects are always references (unless you specifically clone them) I recommend going to php5 if your currently developing new php OO code, this might not be possible due to production env. restriction but it doesn't stop you installing a copy locally and getting your hands dirty :-) have fun.
I.E.: <pseudoCode> class Foo {var $_vars = array();function &setVar1($var) { static $localVars = array(); if (!empty($localVars[$var])) { return $localVars[$var]; } else { $localVars[$var] =& new $var(); return $localVars[$var]; } }function &setVar2($var) { if (!empty($this->_vars[$var])) { return $this->_vars[$var]; } else { $this->_vars[$var] =& new $var(); return $this->_vars[$var]; } }} $result1 =& Foo::setVar1('something'); $bar = new Foo(); $result2 =& $bar->setVar2('something'); </pseudoCode> Right now I'm working on an object controller type of class, but I can see where I might run into this situation in other areas where storing a value in a static function variable or a class variable would accomplish much the same thing as far as the calling code is concerned. Any thoughts? Ed