当我遇到这个函数时,我正试图阅读和理解一些joomla核心PHP代码。它位于libraries/joomla/table/table.php
第268行。在第305行函数的末尾,它返回一个由$tableClass
创建的对象,我不明白的是,这个$tableClass
类定义在哪里?下面是函数的完整列表:
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');
$paths = self::addIncludePath();
$pathIndex = 0;
while (!class_exists($tableClass) && $pathIndex < count($paths))
{
if ($tryThis = JPath::find($paths[$pathIndex++], strtolower($type) . '.php'))
{
// Import the class file.
include_once $tryThis;
}
}
if (!class_exists($tableClass))
{
// If we were unable to find the class file in the JTable include paths, raise a warning and return false.
JLog::add(JText::sprintf('JLIB_DATABASE_ERROR_NOT_SUPPORTED_FILE_NOT_FOUND', $type), JLog::WARNING, 'jerror');
return false;
}
}
// If a database object was passed in the configuration array use it, otherwise get the global one from JFactory.
$db = isset($config['dbo']) ? $config['dbo'] : JFactory::getDbo();
// Instantiate a new table class and return it.
return new $tableClass($db);
}
您可以在任何组件的管理部分的tables子文件夹中找到JTable类。每个文件包含从JTable类扩展而来的表类。你不需要重写这个方法。实际上,JTable可以非常简单。一些例子:
class XXXTableCity extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct( &$db ) {
parent::__construct('#__xxx_cities', 'id', $db);
}
}