Why Adopt a Personal PHP Coding Standard? Boost Efficiency and Readability
This article explains the importance of establishing personal PHP coding standards, covering benefits such as increased efficiency, readability, professionalism, and team collaboration, and provides detailed guidelines on file and folder naming, class and method conventions, indentation, comment styles, directory structure, and overall code style best practices.
Why develop your own coding standards?
Establishing a personal coding standard brings several unexpected benefits: it improves coding efficiency, enhances readability, showcases professionalism, and facilitates team collaboration.
Code Writing Conventions
Folder naming: use all lowercase letters, e.g., controller for a controllers folder.
File naming: class files follow PascalCase (big camel) and end with .class.php (e.g., Session.class.php); utility scripts use camelCase (small camel) like common.php.
Class naming: PascalCase, e.g., Cookie.
Method naming: camelCase, usually verb + noun, e.g., sendMessage. In OOP, protected/private methods may be prefixed with an underscore.
public getUserName()
protected _getTotalAmount()
private _setBlanceAmount()Variable naming: camelCase for regular variables (e.g., $workYears); protected/private properties prefixed with an underscore (e.g., $_salaryAmount).
Constant naming: uppercase with underscores, e.g., define('CURRENT_SCRIPT', 'index.php'); and const TRANSACTION_TYPE = 'income';.
Global variable naming: PascalCase with a leading underscore, e.g., $_System_Config, $_Root_Path.
Indentation: use tabs (or 4 spaces as preferred) for indentation; keep braces on their own line to avoid confusion in nested logic.
Operator spacing: place a single space before and after binary operators.
Control Flow Formatting
Braces occupy a separate line. Example of branch statements:
if($age >= 18 && $age <= 30)
{
echo 'young man';
}
else if($age > 30 && $age <= 60)
{
echo 'middle aged';
}
else
{
echo 'old man';
}
switch($status)
{
case 'forbidden':
echo 'login forbidden';
break;
case 'normal':
echo 'login in';
break;
default:
echo 'status is wrong';
break;
}Loop statements follow the same brace style:
while($condition)
{
// statements
}
foreach($arrayList as $arrayKey => $arrayItem)
{
// statements
}
do
{
// statements
} while($condition);
for($i = $start; $i < $end; $i++)
{
// statements
}Comment Writing Conventions
Comments improve readability, aid code layout planning, and can be processed by tools like phpDocumentor to generate documentation.
Block comments: used for file descriptions, large module overviews, or multi‑line notes.
Line comments: complement block comments for detailed explanations within a module.
Comments should describe *why* something is done rather than *what* the code does.
Example of a phpDocumentor docblock:
/**
* Connect to the database
* @param string $dbhost Database server address
* @param string $dbuser Database username
* @param string $dbpwd Database password
*/PHP Programming Standards
File tags: always use full <?php tags; avoid short tags.
File and directory naming: meaningful English names, using letters, numbers, underscores or hyphens; .php extension required; use camelCase for multi‑word names.
Directory structure: separate directories for app, class, config, data, docs, htdocs, images, css, js, lib, templates, tmp, cache, session, etc., to aid collaboration and maintenance.
Variable naming: start with a letter or underscore, followed by letters, numbers, or underscores; use camelCase; global variables prefixed with an underscore.
Class and interface naming: start with an uppercase letter; multi‑word names use PascalCase; interface names prefixed with an i (e.g., iUserRepository); class name matches file name.
Database naming: all lowercase; table names use underscores; fields use lowercase without underscores; optional prefix with table initial.
Comment standards: place program comments before code, use /** */ for block comments, // for line comments; keep comments concise and focused on purpose.
Code style: use 4 spaces for indentation (or tabs if preferred); surround assignment operators with spaces; limit lines to ~80 characters; break long strings with concatenation; prefer built‑in constants (e.g., PHP_EOL) and commas in echo statements; use printf for formatted output; avoid overusing syntactic sugar.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
