API16

JModel/getInstance

From Joomla! Documentation

< API16:JModel
Revision as of 17:36, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Returns a Model object, always creating it <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<now...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Returns a Model object, always creating it

[Edit Descripton]

Template:Description:JModel/getInstance

Syntax[edit]

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[edit]

mixed A model object, or false on failure

Defined in[edit]

libraries/joomla/application/component/model.php

Importing[edit]

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

Source Body[edit]

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

[Edit See Also] Template:SeeAlso:JModel/getInstance

Examples[edit]

<CodeExamplesForm />