Skip to content

muthukumaran-muthiah/psr-for-cleaner-coding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

  • Files MUST use only <?php and <?= tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
  • Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].
  • Class names MUST be declared in StudlyCaps.
  • Class constants MUST be declared in all upper case with underscore separators.
  • Method names MUST be declared in camelCase.

Example

example1.php...Example1.php

Files

PHP Tags

PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations.

Example

example2.php...Example2.php

Character Encoding

PHP code MUST use only UTF-8 without BOM.

Side Effects

A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.

The phrase "side effects" means execution of logic not directly related to declaring classes, functions, constants, etc., merely from including the file.

"Side effects" include but are not limited to: generating output, explicit use of require or include, connecting to external services, modifying ini settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file, and so on.

Example

example3.php...Example3.php

Namespace and Class Names

Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].

This means each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name.

Class names MUST be declared in StudlyCaps.

Class Constants, Properties, and Methods

The term "class" refers to all classes, interfaces, and traits.

Constants

Class constants MUST be declared in all upper case with underscore separators.

Example

example4.php...Example4.php

Properties

This guide intentionally avoids any recommendation regarding the use of $StudlyCaps, $camelCase, or $under_score property names.

Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.

Methods

Method names MUST be declared in camelCase().

Example

example5.php...Example5.php

PSR: Logger Interface

The LoggerInterface exposes eight methods to write logs to the eight RFC 5424 levels (debug, info, notice, warning, error, critical, alert, emergency).

Methods

  • emergency(string $message, array $context = array()): System is unusable.
  • alert(string $message, array $context = array()): Action must be taken immediately.
  • critical(string $message, array $context = array()): Critical conditions.
  • error(string $message, array $context = array()): Error conditions.
  • warning(string $message, array $context = array()): Warning conditions.
  • notice(string $message, array $context = array()): Normal but significant condition.
  • info(string $message, array $context = array()): Informational messages.
  • debug(string $message, array $context = array()): Debug-level messages.

Example

Logger/

Coding style:

COdingStyle.php

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages