In this page:
The framework has its own autoloader implementation which follows PSR-4 in almost all aspects of autoload. There are some differences which are:
- The autoloader will throw
ClassLoaderExceptionif a class was not found. - It is recommended that a fully qualified class name to have the following form:
VendorName\SubNamespaceNames\ClassName(names uses camleCase).
The framework can be configured to not throw an exception. This can be performed during the process of initializing the autoloader ClassLoader.
Recommended coding style is driven from PHP's recommended style and PSR, but it does not strictly follow it. For this reason, there are few differences. The rules are divided into two types, styles that must always be followed and recommended styles.
- Always use
<?phpinstead of<?for PHP tags. - In case of templates, use the short-hand
echocommand (e.g.<?= 'A a string' ?>instead of<?php echo 'A a string' ?>). - Use 4 spaces for indentation (no tabs).
- Namespaces must be all
Pascal\Case. - Class constants and global constants names must be declared in
ALL_CAPS.
// Correct
define('MY_CONST', 44);
const CLASS_CONSTANT = 44;
// Wrong
define('my_const', 44);
const classConstant = 44;- Methods names, non-static class attributes must be declared in
camelCase.
// Correct
class Hello {
private $fullName;
private $email;
public function helloWorld() {
}
}
// Wrong
class Hello {
private $FullName; // Should be camelCase
private $Email; // Should be camelCase
function helloWorld() { // Missing access modifier
}
}- Classed names, static attributes names must be declared in
PascalCase.
// Correct
class HelloWorldClass {
public static $HelloString;
}
// Wrong
class helloWorldClass { // Should be PascalCase
public static $helloString; // Static attributes should be PascalCase
}- An opening braces
{must be on the same line for functions, method and class declaration. - Never use
elseif. Always useelse if. - Always include access modifiers for class methods, even if they are public.
- Do not use aliases for imported classes or class constants (e.g.
use WebFiori\MyClass as XYZ). - Method names and attributes must be defined as follows, first include access modifier followed by
staticor/thenfinal.
// Correct
public static final function myFunc() {
}
// Wrong
static public final function myFunc() { // Wrong order
final static function myFunc() { // Missing access modifier- For comparison, always use strict comparison (
===or!==) when comparingboolornull. - Do not use the construct
empty()to check fornullvalues. - Use 'null coalescing operator' when needing to use a ternary in conjunction with
isset()
// Correct
$nextLine = $traceEntry['line'] ?? 'X';
// Wrong
$nextLine = isset($traceEntry['line']) ? $traceEntry['line'] : 'X';- Always place PHP code in classes.
- Always specify method parameter's data type.
- Always specify method's return type.
- Limit line length to 120 characters.
- Use meaningful variable and method names that clearly express intent.
- Keep methods focused on a single responsibility.
- Prefer composition over inheritance when possible.
// Good example with type hints and clear naming
public function calculateTotalPrice(array $items): float {
$total = 0.0;
foreach ($items as $item) {
$total += $item->getPrice();
}
return $total;
}- The name of test classes should always follow this syntax:
class XXXTest {
}In this context, the XXX should be replaced by entity name that test class represent. (e.g. GenerateReportTest).
- Name of test methods should follow this syntax:
// Correct
public function testGetData() {
}
public function testGetDataWithInvalidInput() {
}
// Wrong
public function testXXX00() { // Avoid meaningless suffixes
}@testannotation should be included on top of every test method.
The PHPDoc block must be similar to the following style:
/**
* SHORT_DESC.
*
* LONG_DESC.
*
* @annotation1 DETAILS
*
* @annotation2 DETAILS
*/Where:
SHORT_DESCis a short description of the class or method. Must be ended by a dot.LONG_DESCis a big paragraph that contains more details.@annotation1and@annotation2are simple PHPDoc annotations such as@returnor@param.
Notice that each part of the doc block must be followed by an empty space. This helps in improving readability.
- Introduction - Learn about WebFiori framework principles
- Global Constants - Follow naming conventions for constants
- Basic Usage - Apply coding standards in practice
- Folder Structure - Understand code organization standards