Class names in Joomla! Framework

90 views
Skip to first unread message

Stefan Neculai

unread,
Nov 5, 2012, 6:12:47 AM11/5/12
to joomla-de...@googlegroups.com
Hello,

I am wondering why in Joomla classes are named top down and not bottom
up. Is there any specific reason for this or is just the way it was
initially created?

--
Thanks
Stefan Neculai

Sent from my iPhone

Donald Gilbert

unread,
Nov 5, 2012, 9:12:41 AM11/5/12
to joomla-de...@googlegroups.com
I'm not sure I know what you just said.

Stefan Neculai

unread,
Nov 5, 2012, 11:14:36 AM11/5/12
to joomla-de...@googlegroups.com
For example why  UsersControllerProfile in components / com_users / controllers and not ProfileControllerUsers ?

--
Thanks,
Stefan

Herman Peeren

unread,
Nov 5, 2012, 11:17:20 AM11/5/12
to joomla-de...@googlegroups.com
On Monday, 5 November 2012 12:12:51 UTC+1, Stefan Neculai wrote:
I am wondering why in Joomla classes are named top down and not bottom
up. Is there any specific reason for this or is just the way it was
initially created?

Interesting question. I assume you mean why it is JDatabaseQuery and MyextensionModelMymodel, not QueryDatabaseJ and MymodelModelMyextension. And this is not only in Joomla the case: in fact I cannot remember a framework where it is the other way around. Zend, Symfony, Java look around; I'd be very interested in an example of a framework/platform that works bottom up in their naming. So it is a convention. We are used to first think in the big lines and then go down to the details.

Why I think it is interesting to think the other way around? Because the promise of object oriented thinking is: reusable elements. That might be better accomplished by starting at the bottom and building skyhigh from there.

- Herman
 

Donald Gilbert

unread,
Nov 5, 2012, 11:27:55 AM11/5/12
to joomla-de...@googlegroups.com
OK, I see now what you mean.

It is because the UsersControllerProfile is in the file /com_users/controllers/profile.php

You can see in the directory structure very clearly: /com_users/controllers/profile.php

But, as you can see, it doesn't follow the naming directly, as the class name isn't UsersControllersProfile.

This (i think) will be changed in future joomla versions, when the need to prefix components with `com_` disappears and we use things like JLoader::registerPrefix to register our component classes to the autoloader. When that happens, components will be more structured  like the libraries/joomla folder. Like you would know exactly where to find the file that contains the class `JControllerBase`. It resides in: libraries/joomla/controller/base.php

Stefan Neculai

unread,
Nov 5, 2012, 11:35:41 AM11/5/12
to joomla-de...@googlegroups.com
Hey,

@Herman: Well.. I am working on a MVC based on Joomla! and I am not sure what way to chose. I saw in Rouven's example ( https://github.com/realityking/jd12dk/blob/master/Step10/controller/add.php) that he names the controller ControllerAdd (top down). I was using PHPCake and RubyOnRails MVCs before. There, they would name the controller AddController (bottom up) instead.

@Donald: Thanks, but my question was not why this happens but why we are using the top down approach and not the bottom up.

--
Thanks,
Stefan

Herman Peeren

unread,
Nov 5, 2012, 11:42:34 AM11/5/12
to joomla-de...@googlegroups.com
PSR-0, the way many PHP-frameworks are arranging their namespacing (for better exchange, e.g. for autoloading) is also top-down. So it is a bit the general way to do it in PHP, I guess.

Donald Gilbert

unread,
Nov 5, 2012, 11:43:37 AM11/5/12
to joomla-de...@googlegroups.com
We are using top down because it makes sense. As I said in the last paragraph, it makes sense because you know where to find the file that contains the class, based on the class name. For example, it just makes sense that JControllerBase would be located in joomla/controller/base.php.

Does that make sense / answer your question?

Stefan Neculai

unread,
Nov 5, 2012, 11:49:55 AM11/5/12
to joomla-de...@googlegroups.com
Yep, it makes sense. 

I also think that it would be a good idea to add namespaces both in Platform and CMS in the future. I believe it would be much easier to see the structure if there would be Users\Controller\Profile.

--
Stefan

84.le0n

unread,
Nov 5, 2012, 11:56:30 AM11/5/12
to joomla-de...@googlegroups.com
As Donald said with his example, Joomla's autoloader works with this convention.
First J means to load a file under "libraries" folder, then uppercase word mean each directory to look into, until php file is found, creating the entire path of the file that has to be loaded.
This is what I remember, I look at this capability many times ago so I don't know if actually it's changed.


Eng. Gabriele Pongelli

AVVERTENZE AI SENSI DEL D.LGS. 196/2003
Le informazioni contenute in questo messaggio di posta elettronica e negli eventuali files allegati, sono da considerarsi strettamente riservati. Il loro utilizzo è consentito esclusivamente al destinatario del messaggio, per le finalità indicate nel messaggio stesso. Qualora riceveste per errore questo messaggio, Vi preghiamo cortesemente di darcene notizia all'indirizzo e-mail di cui sopra e di procedere alla distruzione del messaggio stesso, cancellandolo dal Vostro sistema; costituisce comportamento contrario ai principi dettati dal D.lgs. 196/2003 il trattenere il messaggio stesso, divulgarlo anche in parte, distribuirlo ad altri soggetti, copiarlo, od utilizzarlo per finalità diverse.
This electronic transmission is strictly confidential and intended solely for the addresses. It may contain information which is covered by legal, professional or other privilege. If you are not the intended addressee, you must not disclose, copy or take any action in reliance of this transmission. If you have received this transmission in error, please notify us and delete the received data as soon as possible.

Donald Gilbert

unread,
Nov 5, 2012, 11:57:39 AM11/5/12
to joomla-de...@googlegroups.com
For sure. Namespacing is on the table for future platform versions, i know. First step in that process is getting all the classes to be autoloadable, and to have one class per file.  That is nearly complete.

Herman Peeren

unread,
Nov 5, 2012, 1:46:26 PM11/5/12
to joomla-de...@googlegroups.com
On Monday, 5 November 2012 17:49:56 UTC+1, Stefan Neculai wrote:
I also think that it would be a good idea to add namespaces both in Platform and CMS in the future. I believe it would be much easier to see the structure if there would be Users\Controller\Profile.

Namespacing is only 3 years old in PHP and for techniques to be used in Joomla! they have to  mature at least 5 years... ;-) 

Note: a "wink" at the end of a sentence means: "don't take it too seriously".

Don

unread,
Nov 5, 2012, 1:50:59 PM11/5/12
to joomla-de...@googlegroups.com
Ya. Haha. I think it's funny that Joomla is talking about implementing namespaces and now officially supporting PHP 5.3, when the rest of the php community has been doing both those things since day 1. PHP 5.3 will be deprecated soon. We should just skip it and go straight to 5.4. 

Sent from my iPhone

Louis Landry

unread,
Nov 5, 2012, 2:10:06 PM11/5/12
to Joomla Platform List
So the answer to the original question is fairly simple.

In the example cited, the Joomla CMS User Component is actually viewed as a separate "app".  The prefix for classes for that "app" is "Users".  Before PHP namespaces existed, Johan and I discussed the standard we would use for namespacing classes using prefixes.  The prefix "J" was reserved for Joomla framework libraries (now Joomla Platform).  We decided to use an "app" prefix for components, such as "Users" for the users component (com_users).  It is obviously the same for the content "app".  There we use the "Content" class prefix.

In a manner of speaking we *do* have namespacing, it just isn't the PHP native namespacing.  Class names are pretty much all namespaced in Joomla, and have been since day 1... just not with slashes because when we started our minimum PHP version requirement was 4.3.  As of this year we (the platform) have set minimum requirements to 5.3, where PHP native namespaces are available.  We certainly want to move in a direction where we leverage the PHP native namespace support, but we also don't want to alienate the millions of lines of code out there written against the current namespacing conventions.  There isn't a clean way (that I've discovered) to offer backward compatibility if we move the codebase to PHP native namespaces so we need to be careful and thoughtful about it.

Cheers.

Donald Gilbert

unread,
Nov 5, 2012, 3:22:53 PM11/5/12
to joomla-de...@googlegroups.com
Louis,

I can see what you mean by "there isn't a clean way to offer backward compatibility." I know we DON'T want to mix the native namespacing with the traditional joomla namespacing, otherwise we would end up with strings like:

Joomla\Controller\JControllerBase
Users\Controller\UsersControllerProfile

I wonder if there could be a way to combine classmaps and some eval processing to the autoloader to accomplish this? Just thinking as I type here, but maybe something like this situation? 

A dev calls the class JControllerBase. It's not loaded yet, so the request goes to the autoloader. The autoloader looks at the classmap of JControllerBase, and loads in the file libraries/joomla/controller/base.php, which contains the namespace Joomla\Controller and the class Base. Then, the autoloader runs an eval to bring in the class like this:

eval('class JControllerBase extends Joomla\Controller\Base {}');

I have no idea what this would be like in terms of real world performance, but it might be worth checking out. This would retain backwards compatibility (in theory) while allowing for a deprecation period on using the "old style namespaced" classnames.

Florian Voutzinos

unread,
Nov 5, 2012, 3:34:40 PM11/5/12
to joomla-de...@googlegroups.com
If Joomla is namespaced (once fully auto loadable), we could provide a cli application that can insert the correct namespace and use statements, and put them in the files headers of applications/components.

We create a map : actual class name => namespaced class name and generate the correct use to place.

For example JControllerBase is found, so it will add "use Joomla\Controller\Base as JControllerBase;" in the file headers and the app code will stay unchanged.

I think that would be a good project for the next GSOC :)
Reply all
Reply to author
Forward
0 new messages