Video Lesson: Create class functions and properties with keyboard shortcuts in PhpStorm

Create class functions and properties with keyboard shortcuts in PhpStorm

Learn how to use keyboard shortcuts to generate class methods, constructors, properties, and dependency assignments in PhpStorm.


Lesson Content

We start with a controller class that implements an interface. PhpStorm marks the class with a red squiggly line because the required method hasn’t been added yet.

Place the cursor inside the class and press Ctrl+Enter to open the Generate menu. Select Implement methods and press Enter:

<?php declare(strict_types=1);

namespace Macademy\PhpStorm\Controllers\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ResponseInterface;

class Index implements HttpGetActionInterface
{
    public function execute()
    {
        // TODO: Implement execute() method.
    }
}

PhpStorm displays the methods that the interface requires. Press Enter to accept the selected method and generate its declaration.

You can use the same Generate menu to add a constructor. Place the cursor after the opening curly bracket of the class, press Ctrl+Enter, select Constructor, and press Enter:

<?php declare(strict_types=1);

namespace Macademy\PhpStorm\Controllers\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ResponseInterface;

class Index implements HttpGetActionInterface
{
    public function __construct()
    {
    }

    public function execute()
    {
        // TODO: Implement execute() method.
    }
}

Now add dependencies to the constructor. This example injects ScopeConfigInterface and PageFactory:

<?php declare(strict_types=1);

namespace Macademy\PhpStorm\Controllers\Index;

use Magento\Cms\Model\PageFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResponseInterface;

class Index implements HttpGetActionInterface
{
    public function __construct(ScopeConfigInterface $scopeConfig, PageFactory $pageFactory)
    {
    }

    public function execute()
    {
        // TODO: Implement execute() method.
    }
}

The constructor parameters exist, but the dependencies aren’t assigned to properties yet. PhpStorm can generate those properties and assignments for you.

Place the cursor inside the constructor parameter list and press Option+Enter on macOS, or Alt+Enter on Windows. Select Initialize properties and press Enter.

PhpStorm opens a dialog containing the available constructor parameters. Press Cmd+A on macOS, or Ctrl+A on Windows, to select both dependencies, then confirm the dialog:

<?php declare(strict_types=1);

namespace Macademy\PhpStorm\Controllers\Index;

use Magento\Cms\Model\PageFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResponseInterface;

class Index implements HttpGetActionInterface
{
    private ScopeConfigInterface $scopeConfig;
    private PageFactory $pageFactory;

    public function __construct(ScopeConfigInterface $scopeConfig, PageFactory $pageFactory)
    {
        $this->scopeConfig = $scopeConfig;
        $this->pageFactory = $pageFactory;
    }

    public function execute()
    {
        // TODO: Implement execute() method.
    }
}

PhpStorm adds the properties and assigns the constructor parameters to them. This shortcut is especially useful when you regularly use dependency injection, since it removes the repetitive work of declaring and initializing each dependency.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!