This method will load a helper, based on the name of the file in which it is located.
The method searches predefined locations of helper files. By default, this is the
helpers??™ folder in the root of the component. To add additional paths, use the
addHelperPath() method.
Using and Building getInstance()
Methods
Many of the core classes in Joomla! use a special method called getInstance().
There are various ways to use this method; we will start by looking at using it to
implement the singleton pattern.
We restrict the instantiation of a class to one of its own member
methods by using the singleton design pattern. This enables us to
create only a single instance of the class, hence the name 'singleton'.
To implement a true singleton pattern, the language must support
access modifiers. If the language does not, we cannot guarantee that
the class will not be instantiated from a different context.
This example shows how we can create a class that, instead of instantiating via the
constructor, we instantiate via the getInstance() method:
/**
* Demonstrates the singleton pattern in Joomla!
*/
class SomeClass extends JObject
{
/**
* Constructor
*
* @access private
* @return SomeClass New object
*/
function __construct() { }
/**
* Returns a reference to the global SomeClass object
*
* @access public
* @static
* @return SomeClass The SomeClass object
*/
function &getInstance()
{
Extension Design
[ 170 ]
static $instance;
if (!$instance)
{
$instance = new SomeClass();
}
return $instance;
}
}
Since we are implementing this as a singleton pattern, we need to prevent the
instantiation of the object outside of the class.
Pages:
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239