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 preparing for archive only |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
===Description=== | ===Description=== | ||
Returns a Model object, always creating it | Returns a reference to the a Model object, always creating it | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
<nowiki>[< | <nowiki>[<! removed edit link to red link >]</nowiki> | ||
</span> | </span> | ||
<! removed transcluded page call, red link never existed > | |||
===Syntax=== | ===Syntax=== | ||
<source lang="php"> | <source lang="php">& getInstance($type, $prefix= '', $config=array())</source> | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 40: | Line 40: | ||
===Source Body=== | ===Source Body=== | ||
<source lang="php"> | <source lang="php"> | ||
function &getInstance( $type, $prefix = '', $config = array() ) | |||
{ | { | ||
$type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type); | $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type); | ||
$modelClass = $prefix.ucfirst($type); | $modelClass = $prefix.ucfirst($type); | ||
$result = false; | |||
if (!class_exists($modelClass)) | if (!class_exists( $modelClass )) | ||
{ | { | ||
jimport('joomla.filesystem.path'); | jimport('joomla.filesystem.path'); | ||
$path = JPath::find( | $path = JPath::find( | ||
JModel::addIncludePath(), | JModel::addIncludePath(), | ||
JModel::_createFileName('model', array('name' => $type)) | JModel::_createFileName( 'model', array( 'name' => $type)) | ||
); | ); | ||
if ($path) | if ($path) | ||
| Line 56: | Line 57: | ||
require_once $path; | require_once $path; | ||
if (!class_exists($modelClass)) | if (!class_exists( $modelClass )) | ||
{ | { | ||
JError::raiseWarning(0, 'Model class ' . $modelClass . ' not found in file.'); | JError::raiseWarning( 0, 'Model class ' . $modelClass . ' not found in file.' ); | ||
return | return $result; | ||
} | } | ||
} | } | ||
else return | else return $result; | ||
} | } | ||
$result = new $modelClass($config); | |||
return $result; | |||
} | } | ||
</source> | </source> | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
<nowiki>[< | <nowiki>[<! removed edit link to red link >]</nowiki> | ||
</span> | </span> | ||
<! removed transcluded page call, red link never existed > | |||
===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> | ||
[[Category:Archived pages API15]] | |||
Latest revision as of 00:58, 25 March 2017
Description
Returns a reference to the a Model object, always creating it
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax
& 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
function &getInstance( $type, $prefix = '', $config = array() )
{
$type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
$modelClass = $prefix.ucfirst($type);
$result = false;
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 $result;
}
}
else return $result;
}
$result = new $modelClass($config);
return $result;
}
[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >
Examples
Code Examples