PHP Good Practice

1)  Do not use relative path in code, as it would not work with Command Line

require_once ‘../../config.php’;

replace it by

require_once dirname(dirname(__FILE__)).’/config.php’;

2) Constant would create a problem in Unit Test, as you would not be able to overwrite in test case

3) Do not use Global variable , as it may change the behaviour randomly if override at somewhere.Even it is problem with unit testing as well. so use dependency injection to do that.

4) Never perform db updates after selecting  records without locks, It will create concurrency issue.

Example: you select 100 records and perform update query on each record without transaction, then if another process do the same, they will conflict. But if you do it by transaction, so table will be locked for that time. So it’s better to set up a flag that you are processing those records and update that flag by select and update lock.

and process those records.

5) Never put string, “specially file path” in double quotes, as some time you may put like “c:\ws\tmp\newfile”. It will be treated as “c:\ws<tab>mp<new line>ewfile” . \t and \n will be treated as newtab and newline.


An E-learning Solution Designer, Technical enthusiastic, LAMP Stack Developer -

Leave a Reply

Your email address will not be published. Required fields are marked *