How do I configure emacs for proper PHP development?

emacs, php

Solution

I use `web-mode` (http://web-mode.org/) for mixed HTML/PHP files and `php-mode` for pure PHP files. The latest version of `php-mode` also recommended `web-mode` for mixed HTML/PHP files: https://github.com/ejmr/php-mode#avoid-html-template-compatibility.

Unlike other modes like `mmm-mode`, `mumamo` or `multi-web-mode` that try to apply different behaviors to different parts of a buffer, `web-mode` is aware of all the available syntax/template that can be mixed with HTML. You can also use `web-mode` for mixed HTML files/templates such as Twig, Django, ERB... In fact I use `web-mode` for anything involve HTML.

There is a catch for PHP template though: Other template systems has different file extension so it is easy to switch the mode automatically, but PHP templates usually use the same `.php` extension; so I have to make it switch by folders or sometimes manually invoke M-x web-mode. Here's my current configuration:

(defun add-auto-mode (mode &rest patterns)
  (mapc (lambda (pattern)
          (add-to-list 'auto-mode-alist (cons pattern mode)))
        patterns))

(add-auto-mode 'web-mode
               "*html*" "*twig*" "*tmpl*" "\\.erb" "\\.rhtml$" "\\.ejs$" "\\.hbs$"
               "\\.ctp$" "\\.tpl$" "/\\(views\\|html\\|templates\\)/.*\\.php$")

BTW, try to separate your PHP files and templates and keep the mixed HTML/PHP file as simple as possible (refactor long PHP blocks into functions in a pure file). The code will be easier to read/follow.

Problem

My current setup in emacs for PHP development has a variety of shortcomings. I often use a mixed mode of html and php. I want the mode to be able to recognize what context I'm in and format appropriately. I am especially interested in appropriate tabbing. This is the most important feature for me. Correct coloring would be nice, but if it messes up once in a while that's ok. I am currently using `multi-web-mode` and the default `php-mode` in Emacs 24.3 on MacOS X. One of the most frustrating problems is incorporating the heredoc syntax: `echo <<<` My current system doesn't recognize that this syntax needs to be NOT tabbed. I typically get warnings like this: ``` Indentation fails badly with mixed HTML/PHP in the HTML part in plaín `php-mode'. To get indentation to work you must use an Emacs library that supports 'multiple major modes' in a buffer. Parts of the buffer will then be in `php-mode' and parts in for example `html-mode'. Known such libraries are: mumamo, mmm-mode, multi-mode You have these available in your `load-path': mumamo ``` I've already tried using mumao/nxhtml but that didn't give me the results I wanted. In some ways it was worse. I'd really appreciate any tips people have for getting a working php development environment setup for emacs.

Original source