Magento 2
PHP 8: Constructor Property Promotion
Free Preview
> PHP 8
<?php
...

class Index implements DataPatchInterface
{
    // Property scope & variables are defined only with constructor arguments.
    // Get used to empty constructor bodies in PHP 8!
    public function __construct(
        protected ModuleDataSetupInterface $moduleDataSetup,
        protected ResourceConnection $resource
    ) {
    }
< PHP 8
<?php
...

class Index implements DataPatchInterface
{
    protected ModuleDataSetupInterface $moduleDataSetup;

    protected ResourceConnection $resource;

    // Properties, class & variable names get referenced in triplicate:
    // 1. Within the property scope definition above.
    // 2. Within the constructor arguments.
    // 3. Then again within the constructor body.
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        ResourceConnection $resource
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->resource = $resource;
    }
Want to learn more?

Visit M.academy to learn much more about Magento, Laravel, PHP, Javascript, & Docker.

M.academy logo