Common mistakes

Thank you for your contribution: 

**FOR SALE**You can already sponsor this chapter!

Starting to develop software is hard. There are so many concepts, ideas, best practices, frameworks and dependencies.

You are usually so happy when your programme simply works. One code statement isn't that complicated. And the "Hello World" example always works well, but when you try to solve "real" problems, you are often lost and after a while you start trying everything to get it to work.

It was the same for me when I wrote this book.

I had so many situations where I didn't know "What is the correct way!".

And often there is no "correct way". People answered my questions concerning coding often with

Well, it depends on ... You can do it like this or like that, but be careful, there can be side effects ...

November 13th I saw this tweet from Radek Suski:

If I see this: http://wklej.org/id/624970/ I think we really need some kind of certification authority for Joomla! developers. #Fail

I asked him whether we could write a chapter on that topic and two weeks later I got his list of common mistakes.

Radek Suski's list of common mistakes

Getting data from request

Most common mistake make by novice Joomla! programmers is the method how they’re getting variables from the HTTP Request:

$id = $_REQUEST[ 'id' ];

That way, besides the fact that it’s not validated, it also isn't determined from what kind of request exactly the data has been taken.
If you are developing a new Joomla! extension you should be certain which way the data is being delivered. For example, if these data are being sent from a form, it's most probable, that these data have been sent via the POST method.

In this case, it would be more appropriated this way:

$id = $_POST[ 'id' ];

However, this variable still isn’t validated. Fortunately the Joomla! framework provides an input class used to manage retrieving data from the HTTP request.

$jInput = JFactory::getApplication()->input;
// From GET
$id = $jInput->get->get( 'id', 0, 'INT' );
// From POST
$id = $jInput->post->get( 'id', 0, 'INT');

As you can see these data are also being validated as an integer variable.
There are also more validation filters available. For more information please visit: http://docs.joomla.org/JInput_Background_for_Joomla_Platform

Connecting to the database

If you need to connect to the database you may have used a method like this:

$dbConn = mysql_connect( 'address', 'login', 'password' );
$db = mysql_select_db( 'table', $dbConn );
$query = "SELECT `data` FROM `jos_my_table` WHERE `name`='{$myName}'";
$results = mysql_query( $query );

First of, all it will not work like this in Joomla! because you don’t know the database name nor the credentials for the db connection. And you don’t really have to.
Here is how it works in Joomla!

// get database object
$db = JFactory::getDbo();
// get new query
$query = $db->getQuery( true );
// what to select
$query->select( 'data' );
// from which table
// do not use fixed db prefix - the #__ will be replaced with the right one
$query->from( '#__my_table' );
// what is the condition
// do not forget to escape any variable you're passing to the SQL-Query
$query->where( 'name=' . $db->escape( $myName ) );
// set the query
$db->setQuery( $query );
// and load result
$results = $db->loadResult();

For more information please visit: http://docs.joomla.org/JDatabase

Files operations

Because Joomla! has implemented an FTP-Layer to avoid possible problems on not properly configured servers it is not recommended to read, and especially to write into a file directly using the native PHP functions. Also, file operations, like creating a new file, copying it, creating a new directory, should be implemented through Joomla! core methods.

So instead of:

$content = "My content";
file_put_contents( $content, 'my_file.txt' );
mkdir( 'new_folder' );
copy( 'my_file.txt', 'new_folder/my_file.txt' );

Use:

jimport( 'joomla.filesystem.file' );
jimport( 'joomla.filesystem.folder' );
$content = "My content";
JFile::write( 'my_file.txt', $content );
JFolder::create( 'new_folder' );
JFile::copy( 'my_file.txt', 'new_folder/my_file.txt' );

Loading styles and scripts

If you would like to add JavaScript or CSS files or CSS declaration the method to do this in Joomla! is quite simple.

// get current document instance
$document = JFactory::getDocument();
// add CSS style declaration
$document->addStyleSheet( 'media/css/my_style.css' );
// add some CSS inline declaration
$document->addStyleDeclaration( 'div#myDiv { border-style: solid; }' );
// add script file
$document->addScript( 'media/js/my_script.js' );
// add inline script declaration
$document->addScriptDeclaration( 'function foo( id ) { alert( id ) }' );

For more information please visit: http://docs.joomla.org/Category:JDocument

Sending Emails

As Joomla! supports already different methods for email communication, it is not recommended to send emails directly using the PHP core functions.

This is the method you probably know:

$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$headers = 'From: webmaster@example.com' . "\r\n" .
       'Reply-To: webmaster@example.com' . "\r\n" .
       'X-Mailer: PHP/' . phpversion();
mail( $to, $subject, $message, $headers );

And here is how you should do this in Joomla!

$mailer = JFactory::getMailer();
$mailer->setSender( array( 'webmaster@example.com', 'John Doe' ) );
$mailer->addRecipient( 'nobody@example.com' );
$mailer->setSubject( 'the subject' );
$mailer->setBody( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' );
$mailer->Send();

In my opinion the Joomla! method is much more elegant. For more information please visit: http://docs.joomla.org/How_to_send_email_from_components

Handling user state information

While developing a script, we sometimes need to store some user state information like, for example, selected ordering, chosen preferences, and so on. Normally, we tend to use HTTP cookies for storing such data. However cookies are quite limited in its functionality and newest HTML5 techniques are not fully supported at the moment.
The Joomla! framework provides an excellent method for this problem. Besides that, the nice feature of this functionality is that we don’t have to worry about the type of the data we want to store. So we can store a string, an array or even an object.

$app = JFactory::getApplication();
// store state data
$app->setUserState( 'my_id', $myVar );
// get stored data
$var = $app->getUserStateFromRequest( 'my_id', 'my_id_in_request', 0, 'int' );

Additionally please note that the “getUserStateFromRequest” method will update the user state variable, if a HTTP request (GET or POST) contains the “my_id_in_request” index so you basically don’t even need to set the state manually.

For more information please visit: http://docs.joomla.org/How_to_use_user_state_variables

YOUR list of common mistakes

I would love to add more of these tips to the list. If you know one, please post it as a comment or contact me.

 

 

Comments

Language Strings. I always get lazy, and leave English strings throughout the code.

Building functionality like dropdown lists - in a helper class instead of using jForm and extend jFormFields.

Overridding methods when it is not relevant.

How to add in edit form an additional data from another edit form. Example : match day edit and include to it events or points per minute