Ctags and Object Oriented PHP5
Saturday, February 11th, 2006
About CTags
Ctags is a great utility. It creates a file with a list of ‘tags’ that are found in various program sources that it finds in the directory where you run it. These tags can then be read by editors so that they know where in the source to find, for instance, the definitions of functions, methods, constants and variables. Or as the CTags homepage describes it:
Ctags generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object).
Editors like Vim use it to let you peek at or jump to the definition of a function; a feature commonly thought to only exist in bloated IDE’s such as Eclipse.
PHP5 Object Oriented
Unfortunately, CTags doesn’t support PHP’s Object Oriented features very well. All you’ll get is globally defined constants and functions. It also generates tags for classes, but that’s it. No support for methods or anything! Hardly very useful in it’s current form, I’d say. Thankfully there is a patch available that greatly extends CTags’ Object Oriented PHP support.
Fixing CTags
To fix CTags so that it does support PHP in a not-completely-retarded fashion, you’ll have to patch and compile CTags yourself. Here’s how:
Download CTags v5.5.4.
Download the patch (alternatively and in case the patch disappears from the previous location, download it from electricmonk’s mirror.
Next, patch, build and install CTags:
[todsah@jib]~/download$ tar -vxzf ctags-5.5.4.tar.gz [todsah@jib]~/download$ patch ctags-5.5.4/php.c ctags-php5.patch [todsah@jib]~/download$ cd ctags-5.5.4 [todsah@jib]~/download/ctags-5.5.4$ ./configure [todsah@jib]~/download/ctags-5.5.4$ make [todsah@jib]~/download/ctags-5.5.4$ sudo make install
The new ctags binary will be installed in /usr/local/bin by default.
Now you can use the self-built version of ctags to create tag files that contain enough information about your PHP’s source code to actually do something meaningful:
[todsah@jib]~/plugwiki$ /usr/local/bin/ctags -R --extra=+q
Fire up your Vim editor (you are using Vim, aren’t you?? Get off my site, you dirty Emacs user!) and start exploiting your newly created tags file.
Using ctags in Vim
In case you don’t know how to use ctags from within Vim, don’t worry. It’s really easy. Move the cursor on top of some method call and press Ctrl-]. Vim will jump to the definition of the method/function/constant/whatever that the cursor was currently on. To jump back to where you came, press Ctrl-t. It’s a simple as that.
An example. Suppose I have the following code:
if (!$page->hasAccess(
I’ll position my cursor on hasAccess and press Ctrl-]. Vim will now jump to this text:
public function hasAccess($user, $access) { $userId = $user->getUserId();
When I type Ctrl-t, Vim will jump back to:
if (!$page->hasAccess(
and I can complete my code.
Another cool tool for Vim that uses tags is TagList.
The “Tag List” plugin is a source code browser plugin for Vim and provides an overview of the structure of source code files and allows you to efficiently browse through source code files for different programming languages.
You can see TagList in action in this screenshot of Vim.
(Thanks to Michiel van Baak for corrections in this post.)