OOP Static Functions vs. Objects
| From: | Edward Vermillion | Date: | Sun, 30 Apr 2006 14:12:51 +0000 |
| Subject: | OOP Static Functions vs. Objects | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-235149@lists.php.net to get a copy of this message | ||
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.
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