Core Development Best Practices

出典: Joomla! ドキュメンテーション

目次

Reduce SQL query count

Great pains should be taken to reduce the amount of queries needed on a page

No queries within loops

If you need information based on a previous query, loop through the results of the first query to find the items needed in the second query and retrieve all that information in one go.

e.g. instead of retrieving user information inside a loop of articles, first retrieve the articles, then find the userid's, you need, and then retrieve user information for the found userid's.

Improve readability

Nobody likes to read a page full of complex processing and SQL queries.

Place complex processing in functions or class methods

If you are doing complex processing, put it in a function or a class method with a descriptive name. If you should need the same processing done later, you have a handy function for it.

Place all SQL queries in functions or class methods

There should be no need for SQL queries outside functions or class methods. It greatly improves readability to have only descriptive calls to functions or class methods instead of SQL queries and subsequent going throught the resultset.

Try to help the PHP parser

When you use the simple PHP syntax to combine variables and string to one string, this causes a lot of parsing work. That's why it is suggested to help the parser at this point and with that increase performance.

Use the right quote type (" " or ' ') for your strings

All use of strings quoted with " (the double quote character) causes the parser to check if there is a variable included and if yes to replace the variable itself with the value of it.

You can help the parser by using the concatenation operator (.), like

$sql = 'select * from mytable where id=' . $whereID;

instead of

$sql = "select * from mytable where id=$whereID";

Please be aware that some characters need to be inside " (double quotes) in order to work:

// The following will not work
$text = 'What ever text\nSecond line of the text';
// This will work
$text = "What ever text\nSecond line of text";
// Alternatively, use concatenation (this might be slower)
$text = 'What ever text' . "\n" . 'Second line of the text';

For more information and examples, see the String documentation.

Use JHTML

Forms should be using the JHTML class. It is an easy way to have a form and will make it easier to modify and add elements to it.


自動更新 2012/05/24 21:35:02