2015-10-22 17 views
1

當我遇到這個函數時,我試圖閱讀和理解一些joomla核心PHP代碼。它位於libraries/joomla/table/table.php行268.在305行函數的末尾,它返回一個由$tableClass創建的對象,我不明白的是,這個$tableClass類在哪裏定義的?以下是功能的完整列表:Joomla 3.4 PHP basic:關於庫/ joomla/table/table.php文件中的JTable :: getInstance

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

回答

1

您可以在任何組件的管理部分找到表子文件夾中的JTable類。每個文件都包含從JTable類擴展而來的表類。您沒有重寫此方法getInstance。事實上,JTable可以非常簡單。例如:

class XXXTableCity extends JTable 
{ 
    /** 
    * Constructor 
    * 
    * @param object Database connector object 
    */ 
    function __construct(&$db) { 
     parent::__construct('#__xxx_cities', 'id', $db); 
    } 
}