Creating Sublime snippets0:00
Let's go through that process of creating a Person again, and we'll see just how time-consuming it really is. We accept a name, and then public function. I'm falling asleep just writing this out, right? And I'm a pretty fast typer, and it's still... if we do it manually, it just takes way too long. Oh my goodness. Instead, we need to speed up this process quite a bit. If you're coming from an IDE, they usually have shortcuts and generators that will do this for you. And the same is true with Sublime. You just need to pull in the proper packages. To start, why don't we create a few snippets? For example, I like being able to type M-E-T for method, and then tab to expand to a full method on my class. Okay, well if I go to tools, you'll see at the very bottom I can create a new snippet, like so. Now notice that we can create stop points by using $ and then wrapping the value and the position within braces. So let's
you'll see at the very bottom I can create a new snippet, like so. Now notice that we can create stop points by using $ and then wrapping the value and the position within braces. So let's do this. I want a public method. We'll have a stop point here, and then we'll have a second stop point here. So that means when I set a tab trigger... and in this case, by the way, I'm hitting command forward slash to toggle the comments. That's very useful. Anyways, if I set a tab trigger of M-E-T, that means when I type it out and hit tab, it will immediately populate this and move the cursor here. I type something in, I hit tab again, and it moves the cursor here. Very, very useful. So we'll set the source or the scope to a PHP file. So we will save this, and you know what? Why don't we create a new folder within our user directory called snippets, and that's where we will store this. I'll call it php-method.sublime-snippet. You must
save this, and you know what? Why don't we create a new folder within our user directory called snippets, and that's where we will store this. I'll call it php-method.sublime-snippet. You must use that extension in order for it to be recognized. All right, so command P, enter. Remember, keep hitting that to go between the two most recently used files. I do that all the time. Okay, so I type M-E-T. Notice that the name of the file is used as a little indicator of what it is, and if I hit tab, it expands. Or I can type age, tab again, and do whatever I need to do there. All right, so that part is done. I no longer have to do this. Gosh, that takes so long. Instead, I can just say M-E-T, name, and then do whatever I need to here. However, what about when I need a protected method? Why don't we modify this? I'm going to go back, and well, we have two options. One, we could create a separate keyboard shortcut like P-M-E-T, and that would be a protected method. Or we could do this.
Public vs protected snippets2:27
don't we modify this? I'm going to go back, and well, we have two options. One, we could create a separate keyboard shortcut like P-M-E-T, and that would be a protected method. Or we could do this here. Now, when you add a position, a colon, and then a value, think of that as the default value. So yeah, we could do this, definitely. Now, if we come back, I could say M-E-T, it defaults to public, but I can change that to protected, and then add it like so. All right, so that's an option. However, I create protected methods enough that it makes sense for me to not type that out every time. So instead, I will copy this, and we'll create a new snippet. I'm going to paste that in, and only change that to protected or private, and update the trigger. All right, so we save it once again to the snippets directory, and we'll say phpProtectedMethod, or we can just remove that entirely and write protectedMethod.sublimeSnippet. All right, so now I think we're good to go.
Generating getters and setters3:22
once again to the snippets directory, and we'll say phpProtectedMethod, or we can just remove that entirely and write protectedMethod.sublimeSnippet. All right, so now I think we're good to go. We can create a public method with M-E-T, tab, or a protected method with P-M-E-T, tab. Next though, and I'm going to bring this back to a regular method. Well, I'm basically just doing a getter here, right? You'll do that a lot. You'll add what translates to a getter or setter. We can streamline this with package control install, and let's look for php, getters, and setters. Very useful. Okay, so now if I'm in a class, and I'll remove that, I can hit Shift Command P and search for getters, and there we go. Generate a getter for a given property, or if I want all getters, then I can use this option here, and that will automatically read your constructor properties and generate the proper getters for them. So I'll show you. Let's
Installing PHP Companion4:13
or if I want all getters, then I can use this option here, and that will automatically read your constructor properties and generate the proper getters for them. So I'll show you. Let's add one more for the age. This age equals age, and this stuff is time-consuming too. We'll clean that up as well. But anyways, if I now say I want all of my getters, it automatically does that for me. Very useful. And actually, the same is true for setters or both of them. So now I have a getter and setter for name as well as age. It saves a huge amount of time. So very useful stuff. Next though, the general process of adding a constructor and accepting the params. It just still takes way too long. Instead, I'm going to pull in a package called php-companion. If you write php, grab this package. It'll save you a huge amount of time. So we'll fetch that. However, once it's pulled in, yeah, they don't give you any kind of readme, which is odd. So instead,
Keybindings for FQCN tools5:04
write php, grab this package. It'll save you a huge amount of time. So we'll fetch that. However, once it's pulled in, yeah, they don't give you any kind of readme, which is odd. So instead, we can just go directly to the source on GitHub. I already have that pulled up. So if we scroll down, we'll first notice there are no key bindings out of the box. You'll just need to create your own. No problem. Very easy. So we have just a handful of commands here, but really useful stuff. Let's start with expandFullyQualifiedClassName. That means if I type, for example, the name of the class minus the full namespace, well, if we trigger this command using a keyboard shortcut, it will automatically populate that for us. So let's try this out. I will copy that command name and we will open up our key bindings, like so. And we'll say, right down here, keys, as well as the command I want to trigger, which is called expandFqcn. All right. So originally,
and we will open up our key bindings, like so. And we'll say, right down here, keys, as well as the command I want to trigger, which is called expand fqcn. All right. So originally, this package included some key bindings, and the binding for this particular command, I believe, was f9. So if you want to add that, this is all you have to do. All right. So if we switch back, let's say we want to pull in some kind of class. It doesn't matter. How about one of Symfony's finder classes? Well, you'd have to know the full path. And if you're not familiar with the component, sometimes you have to copy it, come back, and then you say use, like so, right? It's time consuming. Instead, I could say use finder, I will hit f9 to trigger that command, and notice that it automatically populated. Now, what about the situations where there's more than one finder class? Well, in those situations, like maybe with request, if I hit f9, it will give me
and notice that it automatically populated. Now, what about the situations where there's more than one finder class? Well, in those situations, like maybe with request, if I hit f9, it will give me a choice of which one I want, and then it will fill it in. How useful is that? You'll use this all the time. But now, what if we're doing this after the fact? For example, if I have a constructor, and I want to accept that finder class for some reason for a Person, yeah, well, how would we do that? I can hit f9, but in this case, that's not really what I want. What I wanted to do is import that at the top. So I basically wanted to automatically add that for me. Okay, well, that's where we can use a different command. Let's go back to Chrome, and here's the one we want. Hover over a class name, hit some keystroke that you set up, and it will automatically import it for you. All right, let's try this out. Back to my keybindings, I will duplicate this and change
Hover over a class name, hit some keystroke that you set up, and it will automatically import it for you. All right, let's try this out. Back to my keybindings, I will duplicate this and change this to findUse, and we'll set that to f10 or whatever you want. All right, let's try it. So now, I hover my cursor here, and I hit f10. Now, quick note. Remember, it's possible, depending upon your keyboard settings, if you're on a Mac, that you may have to hit that function key first. It's usually that key in the bottom left-hand corner of your keyboard. So you may have to hit function and then f10. Otherwise, choose a different keybinding that you want. All right, so let's bring this back as it is, and if I hit f10, there we go. We added it at the top. Trust me, if you've been doing this manually before, switch now. This is going to save you an immense amount of time. Okay, so remember those two. F9, as we've said, will expand it. F10 will import it, so to speak.
been doing this manually before, switch now. This is going to save you an immense amount of time. Okay, so remember those two. F9, as we've said, will expand it. F10 will import it, so to speak. What else, though? There's a lot of good stuff here. You could set it up to automatically import or set the namespace for your class. You could insert the constructor properties. And didn't we just say that we wanted that? So let's go ahead and add it. Once again, back here, I will duplicate this, and I'll paste this in. But you know what? I'm pretty sure that's not the right command. If I switch over to Chrome to prove it to you, it looks like their README file is messed up, and actually they forgot to use the proper command name. Anyways, it doesn't matter. If you're working along, just use this one, and we'll map that to F7. Mostly arbitrary. Just pick something that works for you. Now, in our Person class, let's try it out. F7, and notice we get a snippet along with
Constructor properties and workflow9:01
along, just use this one, and we'll map that to F7. Mostly arbitrary. Just pick something that works for you. Now, in our Person class, let's try it out. F7, and notice we get a snippet along with multiple cursors. So if I change this to name, how cool is that? This will save you a lot of time, too. Let's accept an age. F7, age, and you're done. Finally, one more for the gender, and you get the basic idea. Think about how much time that will save you. Alright, so finally our workflow is starting to get there. I'm just going to add a couple more things. One, a snippet for creating a quick class, and another one for generating a constructor, because I may not always do the F7 approach. Alright, so tool, new snippet, and let's start with the class. We'll default to Person just as an example, and then that should be fine. Let's save that to class, and once again scope it to only PHP files. Okay, just a little one, but that'll save you a lot of time. So we'll say new class
as an example, and then that should be fine. Let's save that to class, and once again scope it to only PHP files. Okay, just a little one, but that'll save you a lot of time. So we'll say new class.sublime-snippet. While we're here, why don't we do another one, and this will be for a constructor. So I just want to quickly build this up, and maybe we'll start there and end there. Alright, so for this trigger, I use _C. No real reason, it's just something I've been using for years, and I'm used to it. And again, I will scope that to PHP files. Okay, so we'll say constructor.sublime-snippet, and I think, at least for now, we have everything we need to streamline this rapidly. Let's try it out. class, tab, Person, tab again, but notice it should have brought us here. Let's fix that. We come back, and we'll set a second stop point here. Okay, one more time. Let's get a good workflow. Person, tab, _C, tab, to create a constructor,
Configuring PHP Companion settings10:50
brought us here. Let's fix that. We come back, and we'll set a second stop point here. Okay, one more time. Let's get a good workflow. Person, tab, underscore C, tab, to create a constructor, and we could do it manually if we need to. M-E-T, tab, to create a quick method. P-M-E-T, tab, to create a quick protected method. Or, like we've learned, we could say class, tab, Person, and then I can hit F7 to accept a name, F7 again to accept an age, and then shift command P to add my getters and setters, if that's appropriate. Just a little bit faster than we had before, right? Okay, so in closing, if you don't want these to be private by default, which they are, well, just update one of the settings. Let's go down here, PHP Companion, the default settings. We could update the visibility. So why don't we grab that, but also let's grab use sort length, and that's when we import, it will automatically sort them for us.
the default settings. We could update the visibility. So why don't we grab that, but also let's grab use sort length, and that's when we import, it will automatically sort them for us. So let's try it. We'll go back into our php Companion user settings. I'll paste that in. Let's get rid of these. I will sort according to the length, and then the default visibility will be protected. All right, let's try that one out now. We will start from scratch, F7, name, and notice that it defaults to protected. Next, using that idea of like the Finder class, F7, finder, and then right here I can type finder and F10 to import it at the top. Maybe I also need, and this has nothing to do with a Person class at this point, but maybe a File class. F10, there's multiple choices, so we will pull in maybe this one here. And now notice that it automatically imports them from the shortest length to the longest length. Okay, so I think we've improved our speed and workflow.
