PHPIn this video tutorial, we’ll be looking at three really cool Sublime Text packages that you can install to have a faster PHP Workflow. Open up Sublime Text and let’s get started.

PHP Getters and Setters

First package that I would like to focus on is PHP Getters and Setters. This package enables Sublime Text to generate Getter and Setter methods for your class properties. To install this package follow the below steps.

  1. Open up the Command Palette.
  2. Select Package Control: Install Package.
  3. Search for PHP Getters and Setters package and hit enter to install it.

After the package has been installed. Create a new class or open up an existing class. Create some class properties then by opening up Command Palette and select either of the following options.

  • Generate Getter for … (To Generate a Getter for a Single Property)
  • Generate Setter for … (To Generate a Getter for a Single Property)
  • Generate Getter and Setter for … (To Generate a Getter and Setter for a Single Property)
  • PHP: Generate Getters (To Generate Getters for all properties)
  • PHP: Generate Setters (To Generate Setters for all properties)
  • PHP: Generate Getters and Setters (To Generate Getters and Setters for all properties)

After you select the appropriate option, you’ll immediately see generated getter and setter. Depending on which option you chose.

PHP Constructors

Next, we’re going to be looking at the PHP Constructors package. A handy little package to quickly generate class constructors. Just have a class and define some properties in it. Open Command Palette and select Generate Constructor for Class. You’ll immediately see that Sublime Text generates a default constructor for your class accepting and setting your class properties.

PHP Companion

Next up is the PHP Companion package. This package provides a couple of options that will make your life easier while writing PHP Code with Sublime Text.

Import Namespace

First option is Import Namespace. This option will create the namespace statement for your classes by looking at your directory hierarchy. The way this works is that it will check the first PascalCase directory and will drill down in it to your Class. In this you will notice that it is not writing the root namespace of your application. To fix this, save your directory structure as a Sublime Project and make sure you save the .sublime-project file in your project directory for easy access. Open up the file and add the following code block.

"phpcompanion": {
    "namespace_prefix": "YourAppRootNamespace"
}

Replace YourAppRootNamespace with the actual Root Namespace for your application.

Find Use

Next option is the find use option. How many times this has happened that whenever we have to import a class in our class. We have to open up that class, copy it’s namespace and then paste it in our class’s use statement. With this option, that becomes lightning fast. Just write up the Class name and keep your cursor on the Class Name. Open up Command Palette and select the PHP Companion: Find use option to immediately insert the use statement in your class with the proper namespace.

Implement Interface

When you are creating a class by implementing an interface, it is required that you implement all the methods defined in the interface. In case you don’t do that, PHP will start complaining. The traditional way is to open up the interface, select all the method definitions and paste them in your class. After this you start implementing the methods by writing actual code. By using this option, just keep your cursor on the interface name that you are implementing and choose PHP Companion: Implement Interface option from the Command Palette. This will give you an option to either implement all methods or you can select to implement a single method. After you make your choice, this will insert a dummy implementation with an Exception and you can simple remove that line and start writing the code to implement the method.

Expand Fully Qualified Class Name (expand_fqcn)

A lot of times, we have to import an external class in your class by inlining the full namespace of that class. With this option, it expands the full namespace of the external class inline. Just keep your cursor on the class name and select Expand Fully Qualified Class Name option from the Command Palette. This is great for writing DocBlocks or using external classes inline.

That’s it for this tutorial, in the next tutorial I’ll be showing you more packages by which you can see PHP errors as you write code.

Till next time, Good Bye.