Jump to content

Static functions vs Initialized Object Functions


SchweppesAle

Recommended Posts

Was wonder when is it appropriate to use static functions? 

 

I have an example below, both accomplish the same task, however one requires that an instance of the factory class be created before it can be used.

 

<?php
require_once('testObject.php');

class factory
{
private $_testObject;

function __construct()
{

}

function getTestObject()
{

return $this->_testObject = new testObject();
}

static function getStaticObject()
{
return new testObject();
}

}

?>

Well, the difference stems from the decision you need to make on whether or not you need an instance of a class in order to do the job.  Factories are often abstract with static methods because it's often unnecessary to have to deal with a concrete object for the factory to do its job.  They return concrete product objects, however.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.