SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 229 | Next

James Kennard

"Mastering Joomla! 1.5 Extension and Framework Development"


This example demonstrates how we can use this method:
$anObject =& SomeClass::getInstance();
$anObject->set('foo', 'bar');
$anotherObject =& SomeClass::getInstance();
echo $anotherObject->get('foo');
The two variables, $anObject and $anotherObject, are both pointing to the same
object. This means that the example will output bar.
A similar use of the getInstance() method is to only allow instantiation of one
object per different constructor parameter. This example demonstrates how we can
implement this:
/**
* Demonstrates how to implement getInstance
*/
class SomeClass extends JObject
{
Chapter 7
[ 171 ]
/**
* A private string attribute.
* @access private
* @param string
*/
var $_foo = null;
/**
* Constructor
*
* @access private
* @param string A string
* @return SomeClass New object
*/
function __construct($foo)
{
$this->_foo = $foo;
}
/**
* Returns a reference to a global SomeClass object
*
* @access public
* @static
* @param string A string
* @return SomeClass A global SomeClass object
*/
function &getInstance($foo)
{
static $instances;
$foo = (string)$foo;
if (!$instances)
{
$instances = array();
}
if (!$instance[$foo])
{
$instances[$foo] = new SomeClass($foo);
}
return $instances[$foo];
}
}
Extension Design
[ 172 ]
This example is extremely similar to the singleton example, except we create a
static array to house multiple objects instead of a single object.


Pages:
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
druga wojna światowa Free English grammar and study guid hotel jelenia góra Russian bride counter strike 1.6