filesystem.file');
if(JFile::exists(JPATH_COMPONENT.DS.$file))
{
Chapter 7
[ 173 ]
// file found, let's include it
require_once JPATH_COMPONENT.DS.$file;
if (!class_exists($class))
{
// file doesn't contain the class!
JError::raiseError(0, 'Class '.$class.'
not found.');
return false;
}
}
else
{
// file where the class should be not found
JError::raiseError('ERROR_CODE', 'File '.$file.'
not found.' );
return false;
}
}
$instances[$foo] = new $class();
}
return $instances[$foo];
}
Having explained how to implement the getInstance() methods, we need to
examine why we would need to. There are three main reasons:
This makes it easier to keep track of objects. Take the JDatabase object as an
example. We can access this object at any time using the static JFactory::
getDBO() method. If we were unable to do this, we would need to
continually pass the object around or declare it global in every method and
function that required it.
This helps prevent us from duplicating work. For classes that support it, we
do not have to continually instantiate a new object of that type every time we
need it.
Pages:
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243