Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Adding question mark when nullable value is declared #6033

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Add a new optional parameter to the constructor at the end of the arguments list

In the constructor body, if the new dependency is not provided (i.e. the value of the introduced argument is `null`), fetch the dependency using `Magento\Framework\App\ObjectManager::getInstance()`.

Prefix the type name with a question mark when declaring a parameter with a `null` default value.

{% collapsible Example Code %}

```php
Expand All @@ -134,7 +136,7 @@ class ExistingClass
\Old\Dependency\Interface $oldDependency,
$oldRequiredConstructorParameter,
$oldOptionalConstructorParameter = null,
\New\Dependency\Interface $newDependency = null
?\New\Dependency\Interface $newDependency = null
) {
...
$this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\New\Dependency\Interface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Add a new optional parameter to the constructor at the end of the arguments list

In the constructor body, if the new dependency is not provided (i.e. the value of the introduced argument is `null`), fetch the dependency using `Magento\Framework\App\ObjectManager::getInstance()`.

Prefix the type name with a question mark when declaring a parameter with a `null` default value.

{% collapsible Example Code %}

```php
Expand All @@ -140,7 +142,7 @@ class ExistingClass
\Old\Dependency\Interface $oldDependency,
$oldRequiredConstructorParameter,
$oldOptionalConstructorParameter = null,
\New\Dependency\Interface $newDependency = null
?\New\Dependency\Interface $newDependency = null
) {
...
$this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\New\Dependency\Interface::class);
Expand Down