Class Names in PascalCase
-
Rule: Class names MUST be declared in PascalCase (also known as UpperCamelCase).
-
Reason: PascalCase is the convention for class names in PHP. It helps distinguish classes from other types of variables or functions and maintains consistency with object-oriented practices.
Example (Correct):
class MyClass {
// Class body
}
class UserAccountManager {
// Class body
}
Example (Incorrect):
class myClass {
// Class body
}
class useraccountmanager {
// Class body
}
Method Names in camelCase
-
Rule: Method names MUST be declared in camelCase.
-
Reason: camelCase is the standard convention for method names in PHP. This helps differentiate methods from class names (PascalCase) and makes the code more readable.
Example (Correct):
class MyClass {
public function calculateTotal() {
// Method body
}
public function setUserName() {
// Method body
}
}
Example (Incorrect):
class MyClass {
public function CalculateTotal() {
// Method body
}
public function set_user_name() {
// Method body
}
}
Constant Names in SCREAMING_SNAKE_CASE
-
Rule: Constant names MUST be declared in SCREAMING_SNAKE_CASE.
-
Reason: SCREAMING_SNAKE_CASE is the convention for constants in PHP. It helps distinguish constants from variables and methods and indicates that they are fixed values.
Example (Correct):
class MyClass {
const MAX_USERS = 100;
const DEFAULT_TIMEOUT = 30;
}
Example (Incorrect):
class MyClass {
const maxUsers = 100;
const default_timeout = 30;
}
Variable Names in camelCase
-
Rule: Variable names MUST be declared in camelCase.
-
Reason: camelCase is the convention for variable names in PHP. It maintains consistency across the codebase and helps distinguish variables from constants and methods.
Example (Correct):
$firstName = 'John';
$totalAmount = 500;
Example (Incorrect):
$FirstName = 'John';
$total_amount = 500;
Summary:
-
Class names MUST be in PascalCase.
-
Method names MUST be in camelCase.
-
Constant names MUST be in SCREAMING_SNAKE_CASE.
-
Variable names MUST be in camelCase.