Good guide for own namespaced classes

Be informed that this forum is not an official support forum for Joomla! 4.0. Any issues regarding Joomla! 4.0 must be reported at https://issues.joomla.org/.

Joomla! 4.0 is still in its Alpha stage. This forum should be used for sharing information about Joomla! 4.0.

Moderator: ooffick

Post Reply
Quark Zimmerman
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Sun May 27, 2018 7:58 pm

Good guide for own namespaced classes

Post by Quark Zimmerman » Thu Mar 14, 2019 7:30 pm

I do read documentation :) but what I miss is a good guide for organizing my own library in namespace structure classes.
This should include the registering of classes/namespaces, tips on structure to put them, explanation on how Joomla (or is it PHP?) looks for them and so on.

For example I create my own library to be used by my component, couple of modules that comes with it and admin component.

So, let's start where should I put it in Joomla 4?
Is it same as in Joomla 3.x ? Should I put it in "libraries\MyLibName" ?

Then how I make it "accessible" by my component and it's modules and cetra ?
Should I create a system plugin like in Joomla 3.x that will call something like "JLoader::discover" on onAfterInitialize() event as I did in 3.x? Call something as registerNamespace() with a namespace instead ?
Or there is a better newer way to do it ?
(I do not like the old one since it require to install system plugin first and without it non of my components and modules will work, I would prefer to have some kind of "library" installer and registration)

Ok, done with that, the next question is folder structure.
Let's say I placed some file like this:
"libraries\MyLibName\MyBaseClasses\MyBaseDatabaseClasses\MyDbObject.php"
I assume I have to register namespace with something like "JLoader::registerNamespace("MyLibName","JPATH_LIBRARIES" . "MyLibName")",
right ?
(on this point I would like to understand what 'prs0' and 'prs4' mean, I can read it says 0 for 3.x , 4 for 4.x, but what it actually does and how affects my folder structure and namespace names ?)

But what this actually means ?
For example if I put in code "using MyLibName\MyBaseClasses\MyBaseDatabaseClasses\MyDbObject" (hope I "generated" the namespace "path" properly, that's another thing) does it means it going to actually look into this folder structure ? And "namespace MyLibName\MyBaseClasses\MyBaseDatabaseClasses" I put in my .PHP file actually does nothing besides being informational to which namespace this file belong ?

Or maybe other way around ? By registering the namespace I tell Joomla to scan in subfolders for "namespace MyLibName\MyBaseClasses\MyBaseDatabaseClasses" and such declarations and folder structure is just convenient way to organize classes but has no execution context meaning ?

You see, here are my questions, and I believe such questions will arise from majority of programmers moving to Joomla 4. And unless they answered in a good and clear guidelines or tutorial majority of the people will still use 3.x approach.
Of course the nice thing in Joomla that it completely open sourced, so you can actually discover this answers yourself, but developers should write sites and extensions not spend time revers engineering Joomla.

Quark Zimmerman
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Sun May 27, 2018 7:58 pm

Re: Good guide for own namespaced classes

Post by Quark Zimmerman » Sat Mar 16, 2019 12:21 am

OK, found this:
https://issues.joomla.org/tracker/joomla-cms/18820

While it's a bit outdated and does not answer all my questions it looks like it answers on some of them:

1. The library should be put in "libraries\vendor\<my_company_name>\<my_library_name>" folder.
2. From the investigation of 3.9.4 code (which is probably matches 4.x) and the post it looks like the libraries are support some kin of autoloader , the autoloader itself probably need to be require()-ed ? Then it will load entire library, so we can put it (require_once) in component main file instead of registering in plugin .


Post Reply

Return to “Joomla! 4 Related”