Buffer Yard
Language setup

Emacs PHP Setup: A Language Server, Templates and Standards

Updated

A Laravel project is the realistic test case for PHP in Emacs: controllers and models in plain PHP, views in Blade, a composer.json that pins a coding standard, and a vendor/ directory with thousands of classes you want completion for. A config that handles that handles a WordPress plugin or a Symfony bundle as well.

The most consequential decision comes first, because it shapes everything else: which language server.

Intelephense or Phpactor

Both work with eglot, Emacs’s built-in LSP client since 29.1. They differ in licence, runtime and the depth of what they do for free.

Intelephense Phpactor
Install npm install -g intelephense Composer or the PHAR from the project’s releases
Runtime Node.js PHP
Start command intelephense --stdio phpactor language-server
Licence free core, paid premium features open source
Refactoring rename and more behind the licence class moves, extract method, import fixes

Intelephense is the more common choice and gives good completion out of the box, including for large vendor/ trees. Phpactor needs no Node.js on a PHP-only machine and offers refactorings Intelephense keeps behind its licence. For most people Intelephense is the better default; if you already avoid Node.js or want the refactoring commands without paying, take Phpactor. Switching costs one line.

php-mode

php-mode lives on MELPA and is maintained by the emacs-php organisation. It handles highlighting, indentation for PSR-style code and a few PHP-specific commands, and it is the mode every PHP package in the Emacs ecosystem expects.

Emacs 30 also ships php-ts-mode, a tree-sitter mode. It needs several grammars (PHP itself plus HTML, CSS, JavaScript and PHPDoc for mixed files), so getting it running takes more than one install command. For now php-mode is less hassle; php-ts-mode is worth a look once you are on Emacs 30 and comfortable installing grammars.

php-mode also knows the common house styles. php-mode-coding-style selects indentation and spacing rules, and there are interactive commands for the usual ones: php-enable-psr2-coding-style for most modern frameworks, php-enable-wordpress-coding-style for WordPress plugins and themes (tabs, different brace habits), php-enable-symfony2-coding-style and php-enable-drupal-coding-style. Set the style per project in .dir-locals.el rather than globally, since a WordPress plugin and a Laravel app on the same machine want different answers. The formatter on save will correct whatever slips through, but getting indentation right while you type saves a lot of churn in diffs.

(use-package php-mode
  :ensure t
  :hook (php-mode . eglot-ensure))

(use-package eglot
  :config
  (add-to-list 'eglot-server-programs
               '(php-mode "intelephense" "--stdio")))

For Phpactor the entry becomes '(php-mode "phpactor" "language-server"). Open a file inside the project and eglot starts the server at the project root, which project.el finds through Git. That matters for PHP more than for most languages: the server reads composer.json and the autoload setup from that root, so a PHP app living in a subfolder of a larger repository may need its own root marker. An empty .project file does not help by default; the simplest fix is to open the subfolder as its own Git checkout or to configure project-vc-extra-root-markers (Emacs 29 and later) with composer.json. The first indexing pass over vendor/ takes a while on a big project; after that, completion of framework classes and facades works as expected.

When nothing seems to happen, check the *EGLOT ... events* buffer (M-x eglot-events-buffer). It shows the exact command eglot tried to start and what the server answered, which settles most setup problems in a minute.

Templates go to web-mode

php-mode is good at PHP and poor at HTML with PHP sprinkled through it. Blade, Twig and old-style .phtml views need web-mode, which understands several template engines inside one buffer: it highlights the HTML, the embedded PHP or Blade directives and any inline CSS or JavaScript separately, and indents each part by its own rules.

(use-package web-mode
  :ensure t
  :mode ("\\.blade\\.php\\'" "\\.twig\\'" "\\.phtml\\'")
  :custom
  (web-mode-engines-alist '(("blade" . "\\.blade\\.")))
  (web-mode-markup-indent-offset 2))

The engine list tells web-mode that a .blade.php file is Blade and not raw PHP, so @foreach and {{ $var }} highlight correctly. Twig files are detected by extension. Language-server features do not reach into Blade files with this setup; you get structure and highlighting, and the PHP logic lives in the classes where Intelephense does see it.

The HTML and CSS guide covers web-mode in more detail, including Emmet for writing markup quickly.

Coding standards on the command line and on save

PHP has two common style tools and a project usually commits to one.

PHP_CodeSniffer ships two commands. phpcs reports violations against a standard, and phpcbf fixes what it can. Run it through M-x compile with something like vendor/bin/phpcs --standard=PSR12 src/ and the results land in a compilation buffer where each line jumps to the offending file. Projects often keep a phpcs.xml at the root, in which case plain vendor/bin/phpcs picks up the rules.

php-cs-fixer rewrites files directly. A small after-save hook keeps every saved buffer in style:

(defun my/php-cs-fixer ()
  "Run php-cs-fixer on the file just saved and reload it."
  (when (and buffer-file-name (executable-find "php-cs-fixer"))
    (call-process "php-cs-fixer" nil nil nil "fix" "--quiet" buffer-file-name)
    (revert-buffer t t t)))

(add-hook 'php-mode-hook
          (lambda () (add-hook 'after-save-hook #'my/php-cs-fixer nil t)))

The hook is buffer-local, so it only runs in PHP buffers, and it does nothing when the tool is missing. If the project installs php-cs-fixer through Composer rather than globally, point the function at vendor/bin/php-cs-fixer instead.

Intelephense can also format on request with eglot-format-buffer. Its formatter aims at PSR-12 and offers far fewer options, so on a team with a committed php-cs-fixer config, let the project tool win.

Running tests and the rest

PHPUnit and Pest both run fine from C-x p c (project-compile) with vendor/bin/phpunit or vendor/bin/pest. Failures print file paths with line numbers, and the compilation buffer turns them into links. The phpunit package on MELPA adds commands for the test at point if you want them.

The package guide explains the MELPA setup the snippets above rely on, and the completion page shows how server candidates appear in Corfu or company. If your Laravel project carries a sizeable front end, the JavaScript and TypeScript guide is the next one to read, and the languages overview lists the rest.

Questions readers ask

Is Intelephense free to use in Emacs?

The core of Intelephense is free and covers completion, diagnostics, go-to-definition, hover and formatting. Some features, such as rename and find implementations, need a paid licence key, which you pass in eglot's initialization options. Phpactor is fully open source if that matters to you.

Why does my Blade file open in php-mode instead of web-mode?

Both modes claim files ending in .php, and the first matching entry in auto-mode-alist wins. A :mode entry for \.blade\.php\' in your web-mode use-package block is added at startup in front of php-mode's own entries, so Blade files go to web-mode.

Should I use phpcs or php-cs-fixer?

Use whatever the project already has in its composer.json. phpcs reports violations of a standard such as PSR-12 and its sibling phpcbf fixes them; php-cs-fixer rewrites files according to its own rule sets. Mixing both on one codebase leads to the two tools undoing each other's changes.