Skip to content

Commit

Permalink
Merge pull request #344 from darylldoyle/update/php-namespaces-315
Browse files Browse the repository at this point in the history
Update namespace practices
  • Loading branch information
tlovett1 authored Jun 25, 2021
2 parents a189481 + 4371a10 commit b64c0e1
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions _includes/markdown/PHP.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ Using a common set of design patterns while working with PHP code is the easiest

### Namespacing

We properly namespace all PHP code outside of theme templates. This means any PHP file that isn't part of the [WordPress Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/) should be organized within a namespace or _pseudo_ namespace so its contents don't conflict with other, similarly-named classes and functions ("namespace collisions").
We properly namespace all PHP code outside of theme templates. This means any PHP file that isn't part of the [WordPress Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/) should be organized within a namespace so its contents don't conflict with other, similarly-named classes and functions ("namespace collisions").

Generally, this means including a PHP ```namespace``` identifier at the top of included files:

Expand Down Expand Up @@ -422,22 +422,6 @@ function do_something() {
}
```

If the code is for general release to the WordPress.org theme or plugin repositories, the [minimum PHP compatibility](https://wordpress.org/about/requirements/) of WordPress itself must be met. Unfortunately, PHP namespaces are not supported in version < 5.3, so instead, a class would be used to wrap static functions to serve as a _pseudo_ namespace:

```php
<?php
/**
* Namespaced class name example.
*/
class Tenup_Utilities_API {
public static function do_something() {
// ...
}
}
```

The similar structure of the namespace and the static class will allow for simple onboarding to either style of project (and a quick upgrade to PHP namespaces if and when WordPress raises its minimum version requirements).

Anything declared in the global namespace, including a namespace itself, should be written in such a way as to ensure uniqueness. A namespace like ```TenUp``` is (most likely) unique; ```theme``` is not. A simple way to ensure uniqueness is to prefix a declaration with a unique prefix.

### Object Design
Expand Down

0 comments on commit b64c0e1

Please sign in to comment.