Skip to content

Commit

Permalink
Magic constants (#113)
Browse files Browse the repository at this point in the history
* Add magic constants part to the ::class constant chapter

Co-authored-by: Gary Jones <[email protected]>
Co-authored-by: Juliette <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2022
1 parent 7ee73e7 commit fb0f8b2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion wordpress-coding-standards/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,19 @@ This leaves, for now, only unconditionally declared functions in the global name

Note: Using the `array` keyword in type declarations is **strongly discouraged** for now, as most often, it would be better to use `iterable` to allow for more flexibility in the implementation and that keyword is not yet available for use in WordPress Core until the minimum requirements are raised to PHP 7.1.

### The ::class constant
### Magic constants

The [PHP native `__*__` magic constants](https://www.php.net/manual/en/language.constants.magic.php), like `__CLASS__` and `__DIR__`, should be written in uppercase when used.

When using the `::class` constant for class name resolution, the `class` keyword should be in lowercase and there should be no spaces around the `::` operator.

```php
// Correct.
add_action( 'action_name', array( __CLASS__, 'method_name' ) );
add_action( 'action_name', array( My_Class::class, 'method_name' ) );

// Incorrect.
require_once __dIr__ . '/relative-path/file-name.php';
add_action( 'action_name', array( My_Class :: CLASS, 'method_name' ) );
```

Expand Down

0 comments on commit fb0f8b2

Please sign in to comment.