JModel/getInstance: Difference between revisions
From Joomla! Documentation
New page: ===Description===
Returns a Model object, always creating it
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>Edit Descripton<now... |
m clean up |
||
| Line 2: | Line 2: | ||
Returns a Model object, always creating it | Returns a Model object, always creating it | ||
{{Description:JModel/getInstance}} | |||
{{subst:Description:JModel/getInstance}} | |||
===Syntax=== | ===Syntax=== | ||
| Line 69: | Line 67: | ||
</source> | </source> | ||
{{subst:SeeAlso:JModel/getInstance}} | |||
{{SeeAlso:JModel/getInstance}} | |||
===Examples=== | ===Examples=== | ||
Revision as of 14:14, 24 March 2017
Description
Returns a Model object, always creating it
{{subst:Description:JModel/getInstance}}
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);
}
{{subst:SeeAlso:JModel/getInstance}}
Examples
<CodeExamplesForm />