Using Emacs to edit PHP files

19 03 2009

When we are programming on any language a good editor(emacs :)), lint (which gives syntax errors) and interactive debugger helps us to code fast.

I want to use emacs to edit php files. There are other IDEs which supports lint and debugger but I feel much comfort with emacs. So my idea is to find good php-mode for emacs and I want to find sytax errors in emacs itself. And I want to find good interactive debugger like python debugger.

I found php-mode and compiler for php but I could not find interactive debugger in the command line for php.

PHP-mode for emacs:

http://sourceforge.net/projects/php-mode/

Download php-mode from the above location. Extract the compressed file and copy php-mode.el to emacs lisp path.
Refer this link for emacs lisp path http://www.gnu.org/software/emacs/elisp/html_node/Library-Search.html.
or you can put this php-mode.el file in some directory and you can add this directory to lisp path. To add this new directory to lisp path add this line in the .emacs file.

(add-to-list 'load-path "~/path/to/new/directory/")

And add following lines to .emacs file to enable php-mode

(require 'php-mode)
;; To use abbrev-mode, add lines like this:
(add-hook 'php-mode-hook
'(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))

Enable Tab indentation to 2:

The php-mode indentation for arrays is not good. It will indent like this

$a = array(
           'foo' => 'bar',
           'gaz' => 'gazonk',
          ) ;

If the associated array is having another associative array. then it will be like this

 

$a = array(
           'foo' => array(
                          'bar' => 'bar info'),
           'gaz' => 'gazonk',
          ) ;

If we are having more values like that then the code does not look good. It looks good if it will be like this.

$a = array(
  'foo' => array(
    'bar' => 'bar info'),
  'gaz' => 'gazonk',
) ;

To get view looks like that add following code in .emacs file

(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values

Enable php mode for drupal module files:

To enable php-mode for drupal module file add these lines to .emacs file

(add-to-list 'auto-mode-alist '("/drupal.*\.\(php\|module\|inc\|test\|install\)$" . php-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\.info" . conf-windows-mode))

In place of /drupal you can put your drupal code base path. I put my drupal project at /var/www/ directory I replaced /drupal.* with /var/www/

PHP lint:

Hardly no body use php in the command line, so to use we need to install php-cli (php command line interface). Using php at the command line will make us to find errors easily and fast. We can find syntax error with the following command.
php -l <php file name>

To find more about php-cli option follow this link
http://www.php-cli.com/php-cli-options.shtml

I am using emacs so I can directly check syntax errors in emacs without running the above command on the command prompt. To do that add the following lines in the .emacs file.

;; run php lint when press f8 key
;; php lint
(defun phplint-thisfile ()
(interactive)
(compile (format "php -l %s" (buffer-file-name))))
(add-hook 'php-mode-hook
'(lambda ()
(local-set-key [f8] 'phplint-thisfile)))
;; end of php lint

To check syntax errors press “F8” function key on emacs editor, then currently opened php file will complie and show the syntax errors if any.

PHP Debugger: I tried to find good interactive debugger like python debugger. But I could not find it. So I might be developing one in the future.

Advertisements

Actions

Information

6 responses

20 08 2009
tekai

you can use geben + xdebug to debug your code in emacs

http://code.google.com/p/geben-on-emacs/

20 08 2009
Prajwala

Thanks tekai, I will try this.

27 04 2010
Felix

Thanks this is cool, especially the F8 lint action.

11 02 2011
Bradley

Unfortunately your code for aligning arrays didn’t work for me. That quirk has been bugging me for some time so I was really hoping this would fix it 😦

14 02 2011
Prajwala

Does this link help you?

13 12 2011
Sagar Jauhari

The ‘debug’ option is impressive. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: