klewis
klewis

Reputation: 8360

How to initialize PHP constructor fields in Visual Studio Code

In Visual Studio Code, If I have the following PHP Class

<?php

use ...

class MyHelper
{
  private $logger;

  public function __construct(LoggerInterace $logger) {
  //code...

  }
}

..Is there a way I can tell that constructor to initialize $this->logger = $logger without manually typing that out every time?

Upvotes: 6

Views: 7020

Answers (2)

Shumon Pal
Shumon Pal

Reputation: 423

you can use this PHP Constructor Visual Studio Code package to generate class constructor with variable and it's property. Just install and use shortcut key Ctrl+Shift+P then command Insert Constructor Property. That's it. It will generate what you expect.

Upvotes: 14

Pacynki
Pacynki

Reputation: 1

Try this:

class IMyHelper
{
    private $logger;

    public function __construct(LoggerInterace $logger) {
        $this->logger = $logger
    }
}

And then

class MyHelper extends IMyHelper
{
    // And if here __construct does not exist 
    // php runs constructor from IMyHelper
    // if i remember good :)

  function OtherMethod(){

  }
}

In visual code search plugins but from what vs may know what variable you need inicialize ;)

Upvotes: -3

Related Questions