Lessons: 2Length: 13 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.2 Get Started With PhpStorm

In this lesson, I’ll teach you some of PhpStorm’s key features: including code refactoring, docblock annotation generation, intelligent code completion, and syntax highlighting. I’ll also show you how to use built-in tools such as the database console.

Related Links

1.Get Started With PhpStorm
2 lessons, 12:59

1.1
Introduction
01:26

1.2
Get Started With PhpStorm
11:33


1.2 Get Started With PhpStorm

[SOUND] Hi this is Matthew Snyder for Tuts+ and in this coffee break session I'm going to introduce you to the best IDE PHP available which is PhpStorm. If you're wondering if this course is for you, here's an easy way to know. Are you brand new to PhpStorm? Have you used it but only just done some basic experimentation with it? Are you keen to learn about PhpStorm and see what all the buzz is about? If you can answer yes to any one of these questions then this course is for you. I'm gonna give you a rapid overview of what's on offer. I'm gonna take you through several of the top reasons why PhpStorm is an awesome tool. Dare I say it, the best for developing PHP scripts and applications. Here's reason number one, it features an awesome Smart PHP Code Editor. It has support for PHP 7, specifically 7.1 in the latest release candidate. It also supports name spaces, PHP documentor, code completion, duplicate code detection and Refactoring amongst a range of other features. Let's have a quick look at some of them now by getting set up with an existing code base. With PhpStorm open, I'll choose Open and choose my existing project directory. Notice that while PhpStorm is getting ready, it offers to automatically detect PSR name space roots. I'll choose automatically. And you can see that it's highlighted source and test directories, making it easier to keep abreast of files in the project. With that done, let's have a look at one of the source files where I'll open the journal into the class. You can quickly say that it's done a good job of providing thorough syntax highlighting. From that, you can quickly note the differences between comments, variable names and so on. Now note the use statement at the top of the file. This indicates that Zend form annotation isn't available in PhpStorm's file index. Likely because this is a new project and I've not yet run a composer install on it. So, consequently, the dependency' unavailable. But now note some of the other handy features. You can see line numbers in the left margin along with code folding expanded by default. I'm not sure if you maybe sort of see the point of code folding, but I definitely do. While it's great to see everything, even closing just a few of the falls makes the more important aspects of the file easier to work with. Now let's look a little bit deeper and do some Refactoring. This is perhaps a bit of a fictitious example but it's sufficient to show how PhpStorm works. Here in the populate method, you can see the five member variables are being initialized. Let's say that we want to perform and extract a method refactor. We could do that by hand but we might miss something. Perhaps we're tired, it's late or under stress or so forth. So let's let PhpStorm handle that for us instead. I'll first select the code to extract and then click the Refactor menu then navigate down to Extract > Method. Before we do that though, you can see the PhpStorm has support for all the standard code refactorings. Including moving, changing signatures and extracting constants and variables. Committing to the extract method Refactor, I'll pick a name for the new method. Let's call it something appropriate like InitVariables, and set to be a private method. I'll elect to not generate PhpDoc documentation for the method now and do one more thing which is to refine the parameter's signature. As the data variable is an array, I'll add some type hinting in there. You can see as I do that that the signature preview updates as we change the various settings. Clicking Refactor creates the new method and replaces the initial variable an initialization with a call to the new method. Moving on down to the new method, let's see how to add in that PhpDoc function comment now. This time instead of using our menu, I'll use a keyboard shortcut. On the Mac it's Cmd+N. But check the keyboard shortcuts you're using to know what combination you need, say, if you're on Windows or Linux. Doing this displays a pop up menu where I can generate a number of items. Such as getters and setter, a constructor and PHPDoc blocks. I'll click PHPDoc blocks and it lists all the elements which don't have any as yet. In this case, just our method InitVariables. So clicking OK automatically creates the block for us based upon the function signature. Now let's look at some code completion. One of the main reasons we use IDEs over text editors is such as VIM or Emacs. Let's say that I want to create a new method, one that renders a string representation of our journal entity. To do that, we can implement PHP to string magic method. Let's see what PhpStorm can do to help out with that. I'll start off by getting it to do some text expansion, replacing the text pubf with public function. After that's done, I'll then start to give it a name. You can see that a pop up appears, suggesting the method name from the list of PHP's magic methods. Pressing Tab again completes the name. Clicking Enter after that moves the character to the methods parameters of which I'm not going to add any. Clicking Enter yet again moves the character to the body of the function. Now it's flesh out the body of the method, having it return a string which is generated by passing a call to get class to the employed method where the separator is a new line. When completing both of these methods, I'll use the IntelliSense style technology again to save me time and effort. You can see especially in the case of finding the get_class_vars method that PhpStorm gives an excellent context sensitive list of the appropriate methods. Now what about some further code assistance? Let's say that here in the get created method, which returns the time-stamp when the entry was created as a string, I instead want to return a new date time mutable object. I'll create a new variable called timestamp and start declaring as a new date time object. Again, you can see that the IntelliSense starts listing off the possible matches becoming increasingly specific the more information that I provide. Then once I've picked the one I want from the list, it displays information about the method which it retrieved from an index. Which was supplied by passing in the functions PHPDoc block information. Without looking on php.net or at the function definition, I can know what parameters it accepts, the parameter types, default values and the return type of the function. If at any time later on I want to know that again, I can move the mouse over the function, like so, while pressing Cmd to see the pop-up reappear. All right, you may have seen a small pop-up appear earlier right before I move to the get created method. That's part of PhpStorm's easy code navigation and search functionality. Let's have a look at what I did earlier which is navigating by symbol. In this case, a method name. I press the keyboard shortcut of Alt+Cmd+O and then started typing in the name of the method which I want to move to. As with method names, it provides a known list based again on the project index. As soon as I find the right one, clicking Enter takes me there. This works within files and within the entire project. Alternatively, let's have a look at the Navigate menu. You can see that we can navigate by a host of options, including Class, File, symbol and line. We can move back to the last one where we were or move forward if we move back. We can move to a function or class declaration, parent method and so on. Now, if we have a pretty big project or one that maybe you're new on the project or new to the team which you need to get up to speed with in quite a hurry, this is a huge time saver. I've done it myself on a number of occasions and I can absolutely attest to that. But before we move on, let's have a look at a few of those. I'll move to the definition of date time immutable by holding the mouse over the declaration and Cmd+clicking it. I'll then move back to the general entity definition. I'll move down to getUser ID and then to the ping action. Now let's finish up by looking at some of the integrated tooling. You can see a number of options here at the bottom including TODO, Version Control and Terminal. Clicking Terminal exposes the inbuilt terminal. It's a lot like most terminal emulators that you know, whether that's Mac terminal, GNOME terminal, iTerm2 and so on. It's some quite as full featured but it does an excellent job anyway. From here, let's install the composer dependencies. I'll do that by running php composer.phar with the install command or install switch. Just as you would in the normal terminal, it installs all the dependencies listed in composer.json. And you might be wondering why this is a good thing. If nothing else at least, it saves from using a second application or having to even install one in the first place. With the dependencies installed, you can see that PhpStorm does a quick reindex of the files in the project. And with that done, the in the journal entity now doesn't indicate an error because of a missing file. Quite handy indeed that it keeps everything in sync. Now what about that other mainstay of web-based applications, that being databases? PhpStorm's got a handy integration for that as well. Clicking database here on the right hand side opens up the database plugin where I've preconfigured a connection to a SQL database called SQL.db. You can see that it has one table and a sequence. Expanding the table out, you can see that it has four columns, Id, entry, created and updated. Now let's step a bit further. If I double-click on the table name or click on the table icon here, it will list all the records in the table of which there are none. Right-clicking on the table opens up the context menu pop-up. From here I can do a number of things including modify the table and truncate it. Looking at Modify Table for a moment opens up a DDL editor similar to other tools such as Toad and Navcat. Here, I can alter the column definitions, add and remove keys in this season foreign keys. Now what about running a query? Clicking on the Open Console button here opens a console where I can manually create a SQL query with help from PhpStorm. I'll write a simple query selecting all records from table journal where entry is not null. Note that at every step of the way, PhpStorm provided context sensitive suggestions to help me save time and increase accuracy when writing the query. Now there's far more to the database plugin than I have time to cover, but again it saves you using or installing another tool, at least in the majority of cases. And this has been a rapid introduction to why PhpStorm is not only an excellent IDE, but at least in this person's opinion, arguably one of the best ones around for PHP development. Whether you're a frontend, backend or API developer, there's definitely something for you. What's more, if it doesn't have what you need out of the box, then you're gonna likely find a plugin which will extend the core functionality so that you can do exactly what you need. Check it out at jetbrains.com/phpstorm/download where you'll find an installer for your platform of choice.

Back to the top