Fixing php-mode’s defun handling

Edited 2008-12-28: This issue was fixed in php-mode 1.5.0.

One of the things which I’ve been frustrated with when coding PHP in Emacs is the uselessness of it’s defun-handling functions. Things like mark-defun, narrow-to-defun, beginning-of-defun just didn’t work for me. More precisely, they didn’t work when I was writing OOP code, though they worked for procedural code. Not helpful, since I’m a firm believer in object-oriented programming.

The core of the problem is php-beginning-of-defun-regexp, which doesn’t match many of the keywords available since PHP 5: public, protected, private, static, abstract, or final. I toyed with changing it to match all that, but the number of combinations made it somewhat convoluted. Instead, I went with the rather graceless (though easy) regexp you will find below. It simply ignores anything on the line preceding the function keyword.

(defconst php-beginning-of-defun-regexp
   "^.*function\\s +&?\\(\\(\\sw\\|\\s_\\)+\\)\\s *(")

With this, I’m happily using these functions while writing OOP PHP code.

One Response to “Fixing php-mode’s defun handling”

  1. Atomized » Blog Archive » PHP-Mode 1.5.0 is out Says:

    [...] I missed this, but php-mode 1.5.0 is out, and it fixes bugs. Notably, the *-defun method issues which I’ve mentioned before. [...]

Leave a Reply