Actions

API15

JModel/getInstance

From Joomla! Documentation

< API15:JModel
Revision as of 17:10, 22 March 2010 by Doxiki (Talk | contribs)

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

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

[Edit Descripton]

Description:JModel/getInstance

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;
}

[Edit See Also] SeeAlso:JModel/getInstance

Examples

<CodeExamplesForm />