File-level DocBlock placement with namespace and declare(strict_types=1)

29 Views Asked by At

What is the correct placement of the file-level DocBlock when using declare(strict_types=1) and namespace?

For example is the following correct or does the DocBlock go under the opening PHP tag?

<?php
declare(strict_types=1)

/**
 * DocBlock
 */

namespace Xxx\Xxx;

use Xxx\Xxx;

if (!defined('ABSPATH')) {
    exit;
}

/**
 *
 */
class Xxx {
    // ...
}

1

There are 1 best solutions below

4
AymDev On

Coding style rules are declared in some PSR (PHP Standard Recommendation) standards from PHP-FIG. They are not officially related to the language but are followed by many PHP programmers and can be considered industry standards.

The question you ask is detailed in the PSR-12 - 3rd part:

The header of a PHP file may consist of a number of different blocks. [...] Each block MUST be in the order listed below [...]

  • Opening <?php tag.
  • File-level docblock.
  • One or more declare statements.

[...]

The example that follows shows the file-level docblock before the declare statement:

<?php

/**
 * This file contains an example of coding styles.
 */

declare(strict_types=1);

namespace Vendor\Package;