5

I have very little knowledge about joomla core and PHP. In order to better handle my website, I am trying to read a small part of joomla core, when I come across this function JTable:getInstance, (libararies\joomla\table\table.php line268), it returns

    return new $tableClass($db);

To understand what getInstance returns, I need to know how this $tableClass is defined, so I searched it in all php files in my website, although there are lots of references, even some so-called 'definitions' as this page points out, none of them is what I am looking for. I am expecting something like

class tableClass{...}

Furthermore, from other php files, there are these lines:

$row = JTable::getInstance('K2Item', 'Table');
$row->hit($id);

So it looks to me, what is returned by getInstance should be an object that has a member hit(), so I am expecting something like

class tableClass{...
    function hit(){
    .....
    }
}

But this kind of code is nowhere to be found, so I am stuck here and definitely need help.

Some of my thoughts: Is $tableClass really a class? I notice that it has a $, which all other classes don't have? If it is not a class, then why it could be invoked like this new $tableClass? I really need to understand these basics but it is kind of hard to google keyword $.

6

$tableClass is a variable. You won't find a class named "tableClass". It's just a placeholder for the real class name which will be generated at the beginning of the getInstance method.

Take a look at lines 268-272 where getInstance begins:

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

The first line of this defines which parameters you can pass into getInstance later.

 $type    The type (name) of the JTable class to get an instance of.
 $prefix  An optional prefix for the table class name.
 $config  An optional array of configuration values for the JTable object.

So if you look at the example you gave,

$row = JTable::getInstance('K2Item', 'Table');

"K2Item" is the name of the JTable class ($type) and "Table" is the prefix ($prefix). [$config was not set as it is only optional and probably not needed here]

Line 271 ($type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);) will remove all bad characters from the $type parameter which means every character except letters (A-Z), numbers (0-9) and the characters '_', '.', and '-'. It uses a regular expression for that.

In line 272 $tableClass (which will be the final class name to look after) is generated. It takes $prefix and concatenates the $type from the previous line. ucfirst() is there to convert the first letter of $type to uppercase.

Some examples:

JTable::getInstance('K2Item', 'Table');

$tableClass will become "TableK2Item"

JTable::getInstance('C#!ont&ent');

$tableClass will become JTableContent, because of the bad character removal and $prefix is "JTable" by default.

  • Thorough answer - well done! – FFrewin Aug 20 '15 at 9:41
  • For the record, a shorter pattern that will make fewer potential replacements while maintaining accuracy would be: preg_replace('~[^\w.-]+~', '', $type); ...if anyone wants to suggested it on github. – mickmackusa Nov 28 '18 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.