Skip to content

Commit 80b5fc8

Browse files
authored
Merge pull request #98 from WordPress/import-use-statements
Add import use statements section
2 parents 0e708d6 + 8320c89 commit 80b5fc8

File tree

1 file changed

+75
-0
lines changed
  • wordpress-coding-standards

1 file changed

+75
-0
lines changed

wordpress-coding-standards/php.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,81 @@ Namespacing has no effect on variables, constants declared with `define()` or no
407407
Those still need to be prefixed individually.
408408
[/info]
409409

410+
### Using import `use` statements
411+
412+
Using import `use` statements allows you to refer to constants, functions, classes, interfaces, namespaces, enums and traits that live outside of the current namespace.
413+
414+
Import `use` statements should be at the top of the file and follow the (optional) `namespace` declaration. They should follow a specific order based on the **type** of the import:
415+
416+
1. `use` statements for namespaces, classes, interfaces, traits and enums
417+
2. `use` statements for functions
418+
3. `use` statements for constants
419+
420+
Aliases can be used to prevent name collisions (two classes in different namespaces using the same class name).
421+
When using aliases, make sure the aliases follow the WordPress naming convention and are unique.
422+
423+
The following examples showcase the correct and incorrect usage of import `use` statements regarding things like spacing, groupings, leading backslashes, etc.
424+
425+
Correct:
426+
427+
```php
428+
namespace Project_Name\Feature;
429+
430+
use Project_Name\Sub_Feature\Class_A;
431+
use Project_Name\Sub_Feature\Class_C as Aliased_Class_C;
432+
use Project_Name\Sub_Feature\{
433+
Class_D,
434+
Class_E as Aliased_Class_E,
435+
}
436+
437+
use function Project_Name\Sub_Feature\function_a;
438+
use function Project_Name\Sub_Feature\function_b as aliased_function;
439+
440+
use const Project_Name\Sub_Feature\CONSTANT_A;
441+
use const Project_Name\Sub_Feature\CONSTANT_D as ALIASED_CONSTANT;
442+
443+
// Rest of the code.
444+
```
445+
446+
Incorrect:
447+
448+
```php
449+
namespace Project_Name\Feature;
450+
451+
use const Project_Name\Sub_Feature\CONSTANT_A; // Superfluous whitespace after the "use" and the "const" keywords.
452+
use function Project_Name\Sub_Feature\function_a; // Function import after constant import.
453+
use \Project_Name\Sub_Feature\Class_C as aliased_class_c; // Leading backslash shouldn't be used, alias doesn't comply with naming conventions.
454+
use Project_Name\Sub_Feature\{Class_D, Class_E as Aliased_Class_E} // Extra spaces around the "as" keyword, incorrect whitespace use inside the brace opener and closer.
455+
use Vendor\Package\{ function function_a, function function_b,
456+
Class_C,
457+
const CONSTANT_NAME}; // Combining different types of imports in one use statement, incorrect whitespace use within group use statement.
458+
459+
class Foo {
460+
// Code.
461+
}
462+
463+
use const \Project_Name\Sub_Feature\CONSTANT_D as Aliased_constant; // Import after class definition, leading backslash, naming conventions violation.
464+
use function Project_Name\Sub_Feature\function_b as Aliased_Function; // Import after class definition, naming conventions violation.
465+
466+
// Rest of the code.
467+
```
468+
469+
[alert]
470+
Import `use` statements have no effect on dynamic class, function or constant names.
471+
Group `use` statements are available from PHP 7.0, and trailing commas in group `use` statements are available from PHP 7.2.
472+
[/alert]
473+
474+
[info]
475+
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load whatever is being imported. You'll either need to set up autoloading or load the file containing the class/function/constant using a `require/import` statement, for the aliased constructs to be loaded when used.
476+
[/info]
477+
478+
**Note about WordPres Core usage**
479+
480+
While import `use` statements can already be used in WordPress Core, it is, for the moment, **strongly discouraged**.
481+
482+
Import `use` statements are most useful when combined with namespaces and a class autoloading implementation.
483+
As neither of these are currently in place for WordPress Core and discussions about this are ongoing, holding off on adding import `use` statements to WordPress Core is the sensible choice for now.
484+
410485
## Object-Oriented Programming
411486

412487
### Only One Object Structure (Class/Interface/Trait) per File

0 commit comments

Comments
 (0)