daily-email: add 2023-04-19
This commit is contained in:
parent
08d91ef96e
commit
1c3c355dba
40
src/content/daily-email/2023-04-19.md
Normal file
40
src/content/daily-email/2023-04-19.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
---
|
||||
title: >
|
||||
Camel-case or snake-case for Drupal code?
|
||||
pubDate: 2023-04-19
|
||||
permalink: >
|
||||
archive/2023/04/19/camel-case-or-snake-case-for-drupal-code
|
||||
tags:
|
||||
- drupal
|
||||
- php
|
||||
---
|
||||
|
||||
For some time, [Drupal's PHP coding standards[(https://www.drupal.org/docs/develop/standards/php/php-coding-standards#s-functions-and-variables) allows for writing variables in either snake-case (e.g. `$my_variable`) or lower camel-case `(e.g. $myVariable)`.
|
||||
|
||||
It originally only allowed for snake-case variable names but once it accepted both, I switched to camel-case as my default.
|
||||
|
||||
Why? I didn't like the inconsistency of using one approach for variable names and one for method and property names in PHP classes (which were always camel-case).
|
||||
|
||||
I'd have had code like this with a mixture of both:
|
||||
|
||||
```php
|
||||
class MyClass {
|
||||
|
||||
private EntityTypeManagerInterface $entityTypeManager;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
||||
$this->entityTypeManager = $entity_type_manager;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Or even more simply:
|
||||
|
||||
```php
|
||||
$entity_type_manager = \Drupal::entityTypeManager();
|
||||
```
|
||||
|
||||
I prefer not to have to consistently think about which to use and, if possible, like to use standard approaches in different codebases whether I'm working on a Drupal project, a Symfony project, or a PHP library.
|
||||
|
||||
Plus, I get to use new PHP features like [promoted constructor properties](https://www.oliverdavies.uk/archive/2023/04/12/cleaner-php-code-with-promoted-constructor-properties) if everything is named in the same format.
|
Loading…
Reference in a new issue