A lot of folks in the PHP community have been checking out PHPStorm lately, including myself and most of the developers I work with. We love the code intelligence we get from PHPStorm, but still miss the speed, quick boot-up, & convenience of Sublime Text.
Bạn đang xem: How to run php program in sublime text 3
Before I blindly assume PHPStorm is the only way to go, I wanted to lớn see: Can I bring the things a PHP-focused IDE provides PHP developers back to Sublime Text & get the best of both worlds?
Let"s start with a quick menu of ways that PHPStorm really sets itself apart for me. Please note: There are a million other features that PHPStorm uniquely offers, but khổng lồ be honest, it"s the tiny little conveniences that I"ve seen provide the biggest boost in efficiency.
Also note: This is Sublime Text 3 we"re talking about.
My Must-Haves From PHPStorm
Without most of these wonderful PHP-focused features, it"ll be hard khổng lồ recommend using something other than PHPStorm, even if it"s slower & costlier và uses more memory. So. Can we reproduce them in Sublime Text?Auto-use (import) of classesClass FQCN inline completionEasily navigate to lớn a symbol"s definitionEasy constructor injectionHighlight unused importsGit guttersCode sniffing/PSR-2 validationCode Completion: PHPCode Completion: project code
Before we talk about anything else, you at least need to know how lớn install packages in Sublime Text.
If you haven"t yet, Go install Package Control now.
Unless otherwise specified, every package after this should be installed using Package Control.
Sublime PHP Companion
The most significantly PHP-focused package for Sublime Text is called Sublime PHP Companion.
Like most packages, it contains a series of actions you can perform. ~~They"re mapped to certain keys by default, but you can always re-map them.~~ Update: there is no keymapping by mặc định anymore. Learn more about how lớn set up PHPCompanion keymapping here.find_use (F10) - When your cursor is over a class name, this command makes it simple to lớn use (import) that class.
The package isn"t perfect, and it is clearly not as bright as PHPStorm is when it comes to lớn detecting namespaces and parsing some weird edge cases. But for day-to-day work, this is a huge boost in the PHP-code-knowledge area.
Sublime PHP Companion doesn"t sniff your classes và give you autocompletion, sadly, but SublimeAllAutocomplete does register the names of all symbols (functions, classes, etc.) in any files you have xuất hiện in other tabs and add those lớn the autocomplete register.
This isn"t quite the same as full userland-code-sensitive autocompletion, but it helps a lot.
Cmd-click for function definition
Sublime PHP Companion makes it easy to lớn right click on functions & go khổng lồ their definitions, but this shortcut brings back PHPStorm"s CMD-click-to-definition. FYI, in Sublime Text CMD (or windows" ctrl key or whatever it is on other systems) is called "Super".
First, create a user mousemap file. If you don"t have one, go here:
LinuxCreate mặc định (Linux).sublime-mousemap in ~/.config/sublime-text-3/Packages/User
MacCreate default (OSX).sublime-mousemap in ~/Library/Application Support/Sublime Text 3/Packages/User
WindowsCreate mặc định (Windows).sublime-mousemap in %appdata%Sublime Text 3PackagesUser
Next, place this in the file:
< "button": "button1", "count": 1, "modifiers": <"ctrl">, "press_command": "drag_select", "command": "goto_definition" >You just taught Sublime Text this: "when I hold ctrl và click button one, fire the goto_definition command." Done! (original source)
Note: I originally wanted to lớn suggest using the super modifier, so it would be just lượt thích PHPStorm; however, that would override Sublime Text"s "hold super và click to get multiple cursors" behavior, so I didn"t.
Code sniffing & PHP_CodeSniffer
There"s a package named Sublime PHPCS that brings PHP_CodeSniffer, PHP"s linter, PHP Mess Detector, và Scheck (?) to lớn bear on your code.
You can tweak all sorts of settings, but you"re primarily either going to lớn run it every time you save your file (good, but can get annoying), or every time you trigger it from the command palette (press super-shift-p và then type until you get "PHP Code Sniffer: Sniff this file") or keyboard shortcut (ctrl-super-shift-s by default).
You"ll get gutter highlights and a danh mục up đứng top of all of the places your code doesn"t satisfy the linter.
Note that this and any other packages that rely on code sniffing và linting will be requiring command line applications installed, so be sure to lớn visit their sites & read their directions.
PHP_CodeSniffer Sublime Text 2/3 Plugin
Interestingly, there"s a relatively un-noticed plugin doing the same thing (but for PHPCS only) that"s written by the same group that wrote PHP CodeSniffer, so it might be worth checking out as well; it"s called PHP_CodeSniffer Sublime Text 2/3 Plugin (creative, I know.)
I"ve never used this one, though, so proceed with caution.
Mike Francis PHP CS Fixer Build Script
Mike Francis also shared a custom build script he wrote that runs PHP-CS-Fixer on your code whenever you trigger it. That means it"ll actually enforce PSR-2 (or whatever other PHP-CS-Fixer standard you pass it) on your code for you.
Taylor Otwell actually shared this same script with me, but he didn"t write it up as nicely as Mike did. :) He did, however, mention that you might want to lớn set this preference: "show_panel_on_build": false, This"ll keep it from popping out the command panel with your results every time, which can get very irritating very quickly.
SublimeLinter PHP (and its required dependency, SublimeLinter) rely on PHP"s built-in linter (just like the Sublime PHPCS plugin above). This is a simpler version that only runs the linter, nothing else.
If you"re the type to lớn use PHPStorm, there"s a greater chance that you"re the type lớn write Doc blocks. (Just sayin").
DocBlockr makes it simple to create new doc blocks, but more importantly, if you create a doc block just above a defined function, it will extract that function"s parameter information and pre-fill it in your doc block. Boom.
Sublime Text Git
Are you the type that hates switching from your IDE to lớn your terminal/Git client? Sublime Text Git provides access to many Git commands directly from the Sublime Text command palette.
GitGutter shows you diff information regarding each line"s status--has it been modified, inserted, or deleted?
This is not nearly as powerful as PHPStorm"s Git gutters, but it"s a step in the right direction.
There"s a great plugin that makes it super easy khổng lồ run PHPUnit from the command palette or a keyboard shortcut: SimplePHPUnit
Just lượt thích the name implies, you install the package and you"re up & running.
CodeIntel is supposed lớn provide Sublime Text intelligence about the language you"re working in. It should provide autocompletion, easy jump-to-definition, và information about the function you"re currently working in.
Why vì I keep saying "should" and "supposed to"? Because I have yet lớn meet a PHP developer who can get CodeIntel up & running consistently & predictably. Have you? Hit me up.
When I asked around on Twitter, plenty of folks shared plugins. Since I don"t use these, I can only cốt truyện them vaguely, but I"m sure they"re all worth a quick check.
CodeBug for Xdebug
Do you miss the Xdebug integration in PHPStorm? kiểm tra out Codebug, a standalone xdebug client.
A Few General Sublime Text Tips
This post is not an introduction to all things Sublime Text, but I bởi vì want khổng lồ cover a few important pieces here.
Finding files with "Goto Anything" (cmd-p)
If you press super-P you"ll get the wildly powerful Goto Anything palette, which allows you khổng lồ easily find files, but you can go a bit further: if you find your tệp tin (e.g. By typing Handler.php), you can also trigger opening it at a certain line (Handler.php:35) or at a certain symbol (Handler.php