# Code style used with Table::getInstance i Joomla4

72 views

### Fros...@koonsoft.dk

Nov 10, 2018, 10:25:34 AM11/10/18
to Joomla! CMS Development

Looking at the code style used in the current code may well be a bit confused.

I'm trying to find the best style to copy to the future code.

I'm aware that Joomla4 should still be able to execute  Joomla 3.x code. (more or less)

We must remember that this style will be copied for the next 5 years or more.

You need to select the code style you want.

Example 1:

\$table = Table::getInstance('NewsfeedTable', __NAMESPACE__ . '\\', array('dbo' => \$this->_db));

Here you can see two different code styles

1. Use __NAMESPACE__
2. An array is used ('dbo' => \$ this -> _ db)

Comment to 1)

I prefer not to use __NAMESPACE__, given the use of the JPATH_COMPONENT discussion. It does not take long before this style are copied.

Question to 2)

Usually you use the active database driver object (\$db) or not?

Example 2:

plugins\sampledata\multilong\ multilang.php (1031):

\$ category = Table :: getInstance ('CategoryTable', '\\ Joomla \\ Component \\ Categories \\ Administrator \\ Table \\');

administrator \ components \ com_associations \ Model \ AssociationsModel.php (393):

\$ categoryTable = Table :: getInstance ('Category', 'JTable');

administrator \ components \ com_banners \ Model \ BannerModel.php (128):

\$ categoryTable = Table :: getInstance ('Category');

Comment to Example 2:

Everything else et is a table "class" obejct that we need to have an instance of.

Personally, I personally like it from sample data, as it will look like the one that our components need

\$ table = Table :: getInstance ('{name} Table', '\\ {vendor} \\ Component \\ {componenNeme} \\ Administrator \\ Table \\');

Example 3:

installation\src\Model\LanguagesModel.php(1333):

Comment to Example 3:

I think this code are  not done or it's a mistake.

Since my English is not a strong side, I would like to apologize in advance if my writing style strikes someone. It has nothing proven, but an error on my part

Yours sincerely
Karsten F.

### Michael Babker

Nov 10, 2018, 11:42:35 AM11/10/18
__NAMESPACE__ is a PHP "magic" constant, meaning its value is different based on the class/file it is used in (so for Joomla\CMS\Http\Http the __NAMESPACE__ constant would resolve to Joomla\CMS\Http, for Joomla\CMS\Table\Table it would be Joomla\CMS\Table).  This behavior is similar to __DIR__ or __FILE__ or __CLASS__.  It is nothing like the JPATH_COMPONENT constant which is in essence hardcoded to the active component for a given request.

--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-cm...@googlegroups.com.
To post to this group, send email to joomla-...@googlegroups.com.