Suggestions for coding practices for Joomla 4

Relax and enjoy The Lounge. For all Non-Joomla! topics or ones that don't fit anywhere else. Normal forum rules apply.
Locked
vicn1222
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 128
Joined: Fri Jul 02, 2021 10:34 pm

Suggestions for coding practices for Joomla 4

Post by vicn1222 » Sat Jul 03, 2021 3:59 pm

Just two weeks ago I was searching for a good CMS. I found a lot of people saying how good and easy-to-use WordPress is. So I downloaded and played a few days with it, and then gave up.

I turned to Joomla, and found nothing can match this beauty!

I have written C++ for nearly 30 years. I am a person to go for Qt. I think the mainly Joomla contributors very likely also comes from C++. I use doxygen to generate doc out of this framework. I love it!

Looking at the codes, I do have some small suggestions:

1. For class member variables, prepend "m_" to the variables, such as $m_connection, so people immediately know it is a class member variable.

2. Make all member variable private and have "get" and "set" methods. Of course for C++ this is a must for me, because I use 'ddd' to track who use those member variables (when needed) by putting a debug break in the "get" or "set" method. But not sure how I can debug php and set break points...I currently use printf or var_dump to debug....extremely inefficient...

3. Whenever possible, give a data type to the arguments or function return, such as

public function foo( array& $inputs, string $str ) : array

I am coming from C++ and still learning php, so I could be wrong...

I just wonder why some people say WordPress is better. I guest they are just bloggers and have no knowledge of coding...
Last edited by toivo on Sun Jul 04, 2021 3:07 am, edited 1 time in total.
Reason: mod note: moved from 4.x General Questions, retitled

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24982
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Suggestions for coding practices for Joomla 4

Post by pe7er » Mon Jul 05, 2021 6:35 am

Good to hear that you like the Joomla code. Thank you for your recommendations!

vicn1222 wrote:
Sat Jul 03, 2021 3:59 pm
2. Make all member variable private and have "get" and "set" methods. Of course for C++ this is a must for me, because I use 'ddd' to track who use those member variables (when needed) by putting a debug break in the "get" or "set" method. But not sure how I can debug php and set break points...I currently use printf or var_dump to debug....extremely inefficient...
I think that it's a general PHP recommendation to make your methods private as much as possible.
And only use some public functions that are able to access the private methods.
That way you have more control over what your class is able to do.

Regarding debugging of PHP: you could use Xdebug to debug your PHP code.
It works with most IDEs (PHPStorm, Visual Studio, Netbeans, Eclipse).
I use PHPStorm (commercial software) in combination with
https://xdebug.org/

3. Whenever possible, give a data type to the arguments or function return, such as

public function foo( array& $inputs, string $str ) : array
Actually, that's only possible with the latest PHP versions (since somewhere in the 7.x series) and my PHPStorm does code suggestions for those changes.

If you use another IDE then you could install PHP Code Sniffer (phpcs) in combination with the Joomla code standards, and you will get those suggestions as well.

More info:
https://developer.joomla.org/coding-standards/html.html
https://docs.joomla.org/Joomla_CodeSniffer
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

vicn1222
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 128
Joined: Fri Jul 02, 2021 10:34 pm

Re: Suggestions for coding practices for Joomla 4

Post by vicn1222 » Fri Jul 09, 2021 5:21 am

pe7er wrote:
Mon Jul 05, 2021 6:35 am
Regarding debugging of PHP: you could use Xdebug to debug your PHP code.
It works with most IDEs (PHPStorm, Visual Studio, Netbeans, Eclipse).
I use PHPStorm (commercial software) in combination with
https://xdebug.org/
Hi,

Thank you for the information.

Is there an open source php debugger with gui, like ddd for C/C++?

I install xdebug, but it doesn't seems to have a gui? It probably is like gdb, which we have to use command line to interact with?

I am using CentOS 8.

Thank you.

vicn1222
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 128
Joined: Fri Jul 02, 2021 10:34 pm

Re: Suggestions for coding practices for Joomla 4

Post by vicn1222 » Fri Jul 09, 2021 5:23 am

Actually I don't use any IDE. I use "vi". Very original way...

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24982
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Suggestions for coding practices for Joomla 4

Post by pe7er » Fri Jul 09, 2021 6:53 am

vicn1222 wrote:
Fri Jul 09, 2021 5:21 am
Is there an open source php debugger with gui, like ddd for C/C++?
I don't know.
Xdebug is open source. It is indeed without a GUI.
It interacts between your local web server and your IDE (which usually is a GUI)

I install xdebug, but it doesn't seems to have a gui? It probably is like gdb, which we have to use command line to interact with?

I am using CentOS 8
I use Ubuntu 20.4 with a LAMP stack for the website, PHPStorm (commercial software), XDebug, and a browser (with XDebug plugin) to trigger XDebug on page requests.
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

User avatar
brian
Joomla! Master
Joomla! Master
Posts: 12787
Joined: Fri Aug 12, 2005 7:19 am
Location: Leeds, UK
Contact:

Re: Suggestions for coding practices for Joomla 4

Post by brian » Fri Jul 09, 2021 7:04 am

Well the first recommendation would be to get an IDE. Working with a single file editor is not a good coding practice for non-procedural code
"Exploited yesterday... Hacked tomorrow"
Blog http://brian.teeman.net/
Joomla Hidden Secrets http://hiddenjoomlasecrets.com/


Locked

Return to “The Lounge”