When you ask a PHP developer about such coding, a common excuse is that – it’s a small project they are working on and have no intention to reuse the code in future, however the fact remains, any PHP website development project whether small or big has to be done using standard coding guidelines, if you are a PHP developer.
Whatever coding a PHP developer does needs efforts, and how can you expect all those efforts to go to waste. You might not reuse the code, but it can be someone else referring to it or wanting to use it. They will not be able to use it if it is badly written.
In this blog post we will discuss some fundamental rules of directory structure and security, coding conventions and coding considerations for writing good PHP code.
Very less attention is given to directory submission and filesystem naming conventions in the PHP programming niche making them an area needing prompt attention. Generally, when PHP application development is undertaken all project files are saved on a public web directory, even those which are never ever accessed using the web also find their place on the public web directory.
You will also see different related file types like function libraries, logs, class files, configurations and others, all, jumbled up in either one subdirectory or the main directory. To make things bad these files are saved without proper names, meaning names that do not say anything about the things inside the file, this problem becomes worst when finding a file saved outside of the directory and goes on to become a nightmare as the project grows.
Thus it is very important to make sure that you organize all your project files properly. Directories should be hence named and placed appropriately.
Simple Security Considerations :
This is very important for those parts of the project which are not to be accessed through internet like password protected areas, blocking hotlinking etc. They can be protected by ensuring authentication through .htaccess files.
File Naming Conventions:
How efficiently you will be able to use your PHP application development files depends on their names, it is only when you will be able to find that particular file, that you will be able to use its content.
If not named properly you will actually end up spending lot of time finding the file to be used, to avoid such situations it is better to use appropriate file naming conventions like “word separation” and “camel caps”. When used word separation convention would look something like “new_member_signup.php” and camel caps would look something like “newMemberSignup.php”.
Second rule is not to use the directory name in the files name like “lib.string.php” where you are going to place the file in the lib directory. Apart from the physical application logical application classes, functions, variables and constants should also be named as per the standard conventions.
PHP coding conventions:
No, we are not saying to optimize your code from the time you start writing it, we know that would be very early to start, however following some basic coding conventions never hurts. It will in fact help maximize your code’s efficiency. Some of these rules can be listed as below:
Proper use of single quotes (‘) and double quotes (“), codes in these quotes are interpreted differently in PHP Avoid coding for functions which can be performed by existing function in PHP Choose control structures efficiently for PHP application development Never use register_globals, for it is not only a security risk but will also strip your data of context.
Instead benefit from superglobal variables Avoid using “global” statement, an alternative to global statement is to pass on the object as a reference to a code that depends on the object Use the “include_once” and “require_once” statements to stay clear of duplicate file inclusions Organization is the keyword for effective PHP application development and can be easily done if a PHP developer follows all the basic fundamental rules related to directory structure and security, coding conventions and coding considerations.