JModel/getInstance: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 68: | Line 68: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=getInstance | category=getInstance | ||
category=JModel | category=JModel | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 01:54, 25 March 2017
Description
Returns a Model object, always creating it
Syntax
static getInstance($type, $prefix= '', $config=array())
| Parameter Name | Default Value | Description |
|---|---|---|
| $type | The model type to instantiate | |
| $prefix | Prefix for the model class name. Optional. | |
| $config | array() | Configuration array for model. Optional. |
Returns
mixed A model object, or false on failure
Defined in
libraries/joomla/application/component/model.php
Importing
jimport( 'joomla.application.component.model' );
Source Body
public static function getInstance($type, $prefix = '', $config = array())
{
$type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
$modelClass = $prefix.ucfirst($type);
if (!class_exists($modelClass))
{
jimport('joomla.filesystem.path');
$path = JPath::find(
JModel::addIncludePath(),
JModel::_createFileName('model', array('name' => $type))
);
if ($path)
{
require_once $path;
if (!class_exists($modelClass))
{
JError::raiseWarning(0, 'Model class ' . $modelClass . ' not found in file.');
return false;
}
}
else return false;
}
return new $modelClass($config);
}
Examples
Code Examples