API16

JTable/getInstance: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Static method to get an instance of a JTable class if it can be found in the table include paths. To add include paths for searching for JTable classes JTable::addInclud...
 
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:




<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JTable/getInstance|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JTable/getInstance}}
 
 


===Syntax===
===Syntax===
Line 79: Line 77:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JTable/getInstance|Edit See Also]]<nowiki>]</nowiki>
 
</span>
{{SeeAlso:JTable/getInstance}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=getInstance
  category=getInstance
  category=JTable
  category=JTable
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:09, 25 March 2017

Description

Static method to get an instance of a JTable class if it can be found in the table include paths. To add include paths for searching for JTable classes JTable::addIncludePath().




Syntax

static getInstance($type, $prefix= 'JTable', $config=array())
Parameter Name Default Value Description
$type The type (name) of the class to get an instance of.
$prefix An optional prefix for the table class name.
$config array() An optional array of configuration values for the object.

Returns

mixed A object if found or boolean false if one could not be found.

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

public static function getInstance($type, $prefix = 'JTable', $config = array())
{
        // Sanitize and prepare the table class name.
        $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
        $tableClass = $prefix.ucfirst($type);

        // Only try to load the class if it doesn't already exist.
        if (!class_exists($tableClass)) {
                // Search for the class file in the JTable include paths.
                jimport('joomla.filesystem.path');
                if ($path = JPath::find(JTable::addIncludePath(), strtolower($type).'.php')) {
                        // Import the class file.
                        require_once $path;

                        // If we were unable to load the proper class, raise a warning and return false.
                        if (!class_exists($tableClass)) {
                                JError::raiseWarning(0, 'Table class ' . $tableClass . ' not found in file.');
                                return false;
                        }
                } else {
                        // If we were unable to find the class file in the JTable include paths, raise a warning and return false.
                        JError::raiseWarning(0, 'Table ' . $type . ' not supported. File not found.');
                        return false;
                }
        }

        // If a database object was passed in the configuration array use it, otherwise get the global one from JFactory.
        if (array_key_exists('dbo', $config)) {
                $db = &$config['dbo'];
        } else {
                $db = & JFactory::getDbo();
        }

        // Instantiate a new table class and return it.
        return new $tableClass($db);
}



Examples

Code Examples