General requirements for PHP files

TYPO3 coding standards

The package TYPO3 Coding Standards provides the most up-to-date recommendation for using and enforcing common coding guidelines, which are continuously improved. The package also offers toolchain configuration for automatically adjusting code to these standards. Specifically, a PHP CS Fixer configuration is provided, that is based on PER-CS1.0 (PSR-12) at the time of this writing, and transitioning towards PER-CS2.0.

File names

The file name describes the functionality included in the file. It consists of one or more nouns, written in UpperCamelCase. For example in the frontend system extension there is the file ContentObject/ContentObjectRenderer.php.

It is recommended to use only PHP classes and avoid non-class files.

Files that contain PHP interfaces must have the file name end on "Interface", e.g. EnforceableQueryRestrictionInterface.php.

One file can contain only one class or interface.

Extension for PHP files is always php.

PHP tags

Each PHP file in TYPO3 must use the full (as opposed to short) opening PHP tag. There must be exactly one opening tag (no closing and opening tags in the middle of the file). Example:

EXT:some_extension/Classes/SomeClass.php
<?php
declare(strict_types = 1);
// File content goes here
Copied!

Closing PHP tags (e.g. at the end of the file) are not used.

Each newly introduced file MUST declare strict types for the given file.

Line breaks

TYPO3 uses Unix line endings (\n, PHP chr(10)). If a developer uses Windows or Mac OS X platform, the editor must be configured to use Unix line endings.

Line length

Very long lines of code should be avoided for questions of readability. A line length of about 130 characters (including spaces) is fine. Longer lines should be split into several lines whenever possible. Each line fragment starting from the second must - compared to the first one - be indented with four space characters more. Example:

EXT:some_extension/Classes/SomeClass.php
BackendUtility::viewOnClick(
    (int)$this->pageInfo['uid'],
    '',
    BackendUtility::BEgetRootLine((int)$this->pageInfo['uid'])
);
Copied!

Comment lines should be kept within a limit of about 80 characters (excluding the leading spaces) as it makes them easier to read.

Whitespace and indentation

TYPO3 uses space characters to indent source code. Following the TYPO3 Coding Standards, one indentation level consists of four spaces.

There must be no white spaces in the end of a line. This can be done manually or using a text editor that takes care of this.

Spaces must be added:

  • On both sides of string, arithmetic, assignment and other similar operators (for example ., =, +, -, ?, :, *, etc).
  • After commas.
  • In single line comments after the comment sign (double slash).
  • After asterisks in multiline comments.
  • After conditional keywords like if ( and switch (.
  • Before conditional keywords if the keyword is not the first character like } elseif {.

Spaces must not be present:

  • After an opening brace and before a closing brace. For example: explode( 'blah', 'someblah' ) needs to be written as explode('blah', 'someblah').

Character set

All TYPO3 source files use the UTF-8 character set without byte order mark (BOM). Encoding declarations like declare(encoding = 'utf-8'); must not be used. They might lead to problems, especially in ext_tables.php and ext_localconf.php files of extensions, which are merged internally in TYPO3 CMS. Files from third-party libraries may have different encodings.