Table of Contents

getInstance

API Package Subpackage Class Method Reference Last reviewed Doc status
API Home Package Joomla.Framework Subpackage Application Class JModel Method getInstance Reference getInstance() Never Work in Progress

Returns a reference to the a Model object, always creating it.

The method will try and instantiate an object of the specified model class. The model class name is generated by using the specified prefix and adding on the $type parameter. Note that the $type parameter is first converted to title case, which means that it is all lower case except for the first character, which is upper case. The model type can only contain letters, numbers, ‘_’, ‘-’ and ‘.’. All other characters will be removed.

If the class does not exist, the method will try and load it. It will search the directories that have been specified using the addIncludePath method for files having the same name as the $type parameter. All file names should be lower case.

This method returns an object of the specified model type on success, or false otherwise.

Syntax

JModel &getInstance ( $type, $prefix )

$type string is a string containing the type of model to instantiate.
$prefix string is a string containing the prefix for the model class name. This parameter is optional and if omitted defaults to an empty string.

Examples

Example

jimport( 'joomla.application.component.model' );

class HelloModelHellos extends JModel
{
	function __construct() {
		$config = array(
			'name' => 'MyModel',
			'table_path' => JPATH_COMPONENT.DS.'tables'
		);
		parent::__construct( $config );
	}
}

$model1 =& JModel::getInstance( 'hellos', 'HelloModel' );
$model2 =& JModel::getInstance( '&$%heL@Lo`s', 'HelloModel' );

will create two models, both of class HelloModelHellos (note how the extra characters are removed and case is converted).