How to automate property generation for a class in phpstorm?

房东的猫 提交于 2019-12-04 05:35:49

You can use the Initialize field feature.

This way, you only have to write the constructor method this way:

class SomeNewService {
    public function __construct(TwigEngine $engine, KernelInterface $kernel, LoggerInterface $logger) {        
    }
}

Then you can use initialize fields. Get the cursor over one property of the constructor, then on MacOS use Alt + Enter.

It looks something like this:

After you press enter you are confronted with a list of properties, which you can select by Shift and arrow keys. By selection all the properties, your code will look like this:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!