You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3d5a1b0
Browse filesBrowse the repository at this point in the historyBrowse files
Tomáš Votruba
authored
Merge pull request zedr#26 from peter-gribanov/default_arguments
Copy file name to clipboardExpand all lines: README.md
+25-7Lines changed: 25 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,22 +186,40 @@ class Car
186
186
187
187
### Use default arguments instead of short circuiting or conditionals
188
188
189
-
**Bad:**
189
+
**Not good:**
190
+
191
+
This is not good because `$breweryName` can be `NULL`.
192
+
190
193
```php
191
-
function createMicrobrewery($name = null) {
192
-
$breweryName = $name ?: 'Hipster Brew Co.';
193
-
// ...
194
+
function createMicrobrewery($breweryName = 'Hipster Brew Co.')
195
+
{
196
+
// ...
194
197
}
195
-
196
198
```
197
199
198
-
**Good**:
200
+
**Not bad:**
201
+
202
+
This opinion is more understandable than the previous version, but it better controls the value of the variable.
203
+
199
204
```php
200
-
function createMicrobrewery($breweryName = 'Hipster Brew Co.') {
205
+
function createMicrobrewery($name = null)
206
+
{
207
+
$breweryName = $name ?: 'Hipster Brew Co.';
201
208
// ...
202
209
}
210
+
```
211
+
212
+
**Good**:
213
+
214
+
If you support only PHP 7+, then you can use [type hinting](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) and be sure that the `$breweryName` will not be `NULL`.
203
215
216
+
```php
217
+
function createMicrobrewery(string $breweryName = 'Hipster Brew Co.')
0 commit comments