Automatic YASnippets for PHP in Emacs
The php-auto-yasnippets package for GNU Emacs will automatically create ‘snippets’ for standard PHP functions. It defines these snippets using the YASnippet package, with optional support for auto-complete. For example, if you type the PHP function
C-c C-y then php-auto-yasnippets will expand that into
with the cursor ready to overwrite
$glue with the string you want to
use. Pressing Tab will skip over to
$pieces to do the
same. This way you can be sure you not only have the correct number
of arguments, but also that you have them in the correct order. PHP
comes with a large standard library and functions that sound similar
sometimes require arguments in contrasting orders. This package will
help you avoid having to remember those corner cases.
If a function has any optional parameters then php-auto-yasnippets
will wrap them in square braces. This is the same convention the PHP
manual uses to indicate optional parameters. For example,
php-auto-yasnippets will expand
filter_input into this:
filter_input($type, $variable_name, [$filter], [$options])
If you do not need the optional parameters you can delete them by
C-d when you Tab over to them.
You can use the prefix command to expand method names. When you use
the prefix, u.g.
C-u C-c C-y, the package will ask you for the name
of the class which implements that method. This information is
necessary in order to generate the correct snippet.
To use php-auto-yasnippets you need to do three things. First, place
the package in your load-path (
C-h v load-path for help) and load it
from your Emacs configuration file by adding:
Second, make sure the variable
to the program
Create-PHP-YASnippet.php. That PHP program should
have come with this package; if you do not have it then you can get it
from the project website. By default php-auto-yasnippets will
search for the PHP program in the same directory as the Elisp code,
php-auto-yasnippets.el. If you want to put the PHP program
in another place then use
setq in your configuration file to set the
variable to the proper path, e.g:
(require 'php-auto-yasnippets) (setq php-auto-yasnippet-php-program "~/path/to/Create-PHP-YASnippet.php")
Finally, bind the function
yas/create-php-snippet to a key of your
choice. You must do this because php-auto-yasnippets defines no
key-bindings. And since the package requires php-mode, and is most
useful when writing PHP code, you may want to use a key-binding that
only works when using php-mode. For example:
(define-key php-mode-map (kbd "C-c C-y") 'yas/create-php-snippet)
Now if you type the name of a PHP function and press
C-c C-y it will
expand into a snippet containing all of the parameters, their names,
any default values, et cetera.
With auto-complete support activated, it's even simpler:
Return (to choose the first completion) expands the
First, install and configure auto-complete.
to your php-auto-yasnippets setup, and you should be good to go.
Note that auto-completion does not support user-defined functions or classes (it relies on auto-complete's php-mode dictionary of functions).
Loading User Code
This package can also generate snippets for user-defined functions and methods.
You can tell a buffer what files to load for function and class definitions by
php-auto-yasnippet-required-files to a list of required paths.
If you use Composer, you might put something like
(php-mode . ((php-auto-yasnippet-required-files (list "~/project/vendor/autoload.php"))))
Now you can generate snippets for any classes Composer autoloads, in any PHP file in the project.
- Glynn Forrest
- Yasuyuki Oka
- Steve Purcell
- Nate Eagleson
- Alessandro Madruga Correia
- Michel Bystranowski
This package uses Semantic Versioning.
Copyright 2013, 2014 Eric James Michael Ritz