Enabling code completion for PHP-GTK in your PHP IDE

One of the most useful aspects of Integrated Development Environments (IDE), like Zend Studio or Eclipse PDT, is the ability to obtain code completion information when typing PHP source.

Regrettably, however, none of these IDEs is by default instrumented to provide code completion for PHP-GTK developers. But there is a way to make them seem PHP-GTK-aware.

How it works

These IDEs are able to provide code completion not only for prebuilt extension code, but also for user-level PHP code. This gives us a way to provide it to PHP-GTK developers : all it takes is a "stub" file containing PHP declarations equivalent to the PHP-GTK2 classes, interfaces, and their methods and constants.

Due to the sheer volume of the PHP-GTK API, it is out of the question to build such a stub file by hand, as this would be too much work and almost immediately obsolete, but luckily the PHP5 Reflection API enables reverse-engineering the PHP-GTK extension interface back to a PHP file that will be mostly equivalent to the original API.

Generating the stub file

The attached Gtk_Dumper script produces such a stub file.

  1. Running the script from the command line with a PHP-GTK2-enabled PHP generates a PHP source for PHP-GTK2 on the standard output
  2. Save this output to a file, say php-gtk-skeletons.php, preferably in a separate directory.
  3. Reference this file in the PHP-GTK projects within your IDE. Actual steps detailed in the following pages.
  4. Do NOT include/require the file in your PHP-GTK2 scripts : its definitions would be in conflict with the actual definitions from the extension


There are a few limitations due to differences between C (in which GTK and PHP extensions are coded), and PHP itself:

  • The script can only generate stubs for extensions currently enabled. This means that if you install a new PHP-GTK extension (like Glade which is no longer bundled in PHP-GTK 2.0.1), you'll have to rerun the script in order for the Glade prototypes to be listed. The attached prototypes file is for the bare default PHP-GTK 2.0.1
  • Some classes implement the Traversable Interface, which can simply not be created in user PHP code. The prototypes are therefore missing it, but the information is available as comments in the stub file
  • Some classes define constants or method with reserved names:
    • foreach() and unset() are replaced by foreach_method and unset_method() wherever they are appear. Remove _method in your PHP-GTK programs
    • Gdk::XOR, Gdk::AND and Gdk::OR are replaced by Gdk::XOR_Blit, Gdk::<code>AND_Blit, and Gdk::OR_Blit respectively. Remove _Blit in your PHP-GTK programs
  • the generated prototypes are those of the version on which you ran the dumper script. If you install a new PHP-GTK2 version, remember to rerun the script, in order to take into account the changes in the PHP-GTK2 API
php-gtk-prototypes.zip58.14 KB
Gtk_Dumper.php.txt24.92 KB