Page tree

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Every developer should follow the PHP Standards Recommendations

1.1 Variables:

  • Use camelCase for variable names.
  • Variable names should be descriptive and concise.
  • Avoid abbreviations unless widely recognized.


Example
$userName = 'John';
$totalAmount = 100;


1.2 Functions and Methods:

  • Use camelCase for function and method names.
  • Function and method names should clearly express their purpose.
  • Methods should start with a verb when possible.


Example
function calculateTotalAmount($prices)
{
    // Logic here 
}


1.3 Classes:

  • Use PascalCase for class names.
  • Class names should be nouns and describe the entity or the functionality.


Example
class UserController 
{ 
    // Class definition
}


1.4 Constants:

  • Use UPPER_CASE with underscores (screaming snake case) for constants.
  • Constants should be descriptive and represent fixed values.



Example
define('MAX_RETRY_ATTEMPTS', 5); 
const DEFAULT_TIMEOUT = 30;

1.5 Interfaces:

  • Use PascalCase with the suffix Interface.
  • Interfaces should describe behavior and represent contracts.


Example
interface CacheInterface 
{ 
    public function get(string $key): int; 
    public function set(string $key, string $value); 
}


1.6 Abstract classes

  • Use PascalCase with the prefix Abstract.


Example
abstract class AbstractJob
{
    abstract public function getName(): string;
    // Other logic here
}


1.7 Namespaces:

  • Use PascalCase for namespaces.
  • Namespace names should map to the directory structure.


Example
namespace App\Controllers;





Principles:

  • YAGNI: You ain’t gonna need it (wiki)
  • KISS: Keep It Simple, Stupid (wiki)
  • DRY: Don’t Repeat Yourself (wiki)
  • SOLID: (wiki)
    - Single-Responsibility-Prinzip
    - Open-Closed-Prinzip
    - Liskovsches Substitutionsprinzip
    - Interface-Segregation-Prinzip
    - Dependency-Inversion-Prinzip


  • No labels