Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate property generation for a class in phpstorm?

If I implement a class, which gets some services injected, I have to write this bulk of code:

<?php
namespace Hn\AssetDbBundle\Services;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpKernel\KernelInterface;

/**
 * Class SomeNewService
 * @package Hn\AssetDbBundle\Services
 */

class SomeNewService {
    /**
     * @var TwigEngine
     */
    private $engine;
    /**
     * @var KernelInterface
     */
    private $kernel;
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(TwigEngine $engine, KernelInterface $kernel, LoggerInterface $logger) {
        $this->engine = $engine;
        $this->kernel = $kernel;
        $this->logger = $logger;
    }
}

This seems redundant. Is there a way I can reduce the amount of code I have to write? Is there a live template for initializing the fields or can I autogenerate the bulk of this block otherwise?

like image 634
k0pernikus Avatar asked Oct 02 '14 16:10

k0pernikus


People also ask

How do I get constructors in Intellij?

You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select Constructor , Getter , Setter or Getter and Setter .

How do you create a constructor in PyCharm?

As you type the argument name, hit Alt-Enter and choose Add 'field' to constructor , then type the next argument. Or later, put the cursor on each argument and invoke it. PyCharm will create the assignment for you.


1 Answers

You can also do the other way around, defining the properties first, and then in the "Generate" menu (Cmd+N), use "Constructor".

like image 184
whitezo Avatar answered Sep 19 '22 15:09

whitezo