Ways to code PHP

May 29th, 2008

Prolog: In the last few years, I wrote PHP code mostly within Notepad. At some point I felt a strong desire for syntax highlighting and a structured source tree and therefore I am still exploring the possibilities.

As the first real need was syntax highlighting, I ended up using SciTE first. It is pretty much a more extended Notepad with the ability to highlight PHP code via a syntax highlight definition that ships with it. However, SciTE has too many different half-working highlight definitions and seems not to like wide pages so that scrolling to a far-right-away position in the middle of a line is somewhat painful. Maybe they fixed this already, but the last version I used still did so. There is also no source tree for a project folder.

Afterwards I read about Textmate, a much praised editor for Mac and I liked the screenshots/screencasts (my first from the Ruby “A blog in 15 minutes” buzz). It comes with a source tree view and pretty syntax highlighting. Unfortunately it is a commercial Mac only product and if you own one, you will pretty sure already know about it.

Not much later, E - Texteditor, which claimed to be Textmate for Windows, appeared and I gave it a try. In fact it was not hard to set it up using a really nice look and it was much fun to use it. However, there is no php documentation available directly from the editor - ok, it’s not an IDE, it’s an editor. In the first releases there were some bugs and there still seem some to be left especially when quickly closing and reopening E, but I can recommend this editor to everyone who has $ 34,95, wants a very pretty editor and currently misses syntax highlighting and a source view. Imho E is the best Notepad replacement for editing PHP code available on Windows and it can also do much more.

On Linux, gedit is an excelent tool that also ships with gnome. It’s basically the same like E but does not look so pretty. However, editing PHP files with it does just work and there has been some progress recently to make PHP highlighting more comfortable. You may need to tweak it a little to fit your needs, but because you are using Linux, I assume you know where to find more information on this :P.

Not long ago I also noticed Netbeans’ approach to PHP editing and because I have been using Netbeans for a long time to write Java already, it was a must to try it out. At the first look it is just good old Netbeans as you know it from Java but the Early Access for PHP edition features PHP only instead including code completition and so on. Unfortunately creating a new project from existing source folders is not yet trivial and there are some settings that you will never need without appserver integration. However it is possible to create a new project just by using the default settings, steal the “nbproject” directory from it, copy it to the root folder of your PHP app and edit the “project.properties” and “project.xml” to fit your needs. For example:

C:\yourapp\nbproject\project.properties:
copy.src.files=false
copy.src.target=
include.path=\
    ${php.global.include.path}
index.file=index.php
source.encoding=UTF-8
src.dir=C:\\yourapp
url=http://localhost/yourapp_path_maybe

C:\yourapp\nbproject\project.xml:
<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://www.netbeans.org/ns/project/1″>
    <type>org.netbeans.modules.php.project</type>
    <configuration>
        <data xmlns=”http://www.netbeans.org/ns/php-project/1″>
            <name>YourappName</name>
        </data>
    </configuration>
</project>

Done so, when opening the project (just the root folder of your app) you are presented with your PHP application loaded inside Netbeans with all your files in the source tree view. From now on you can edit your project easily and, if you setup a local webserver (take a look at Server2Go or Lighty2Go if you want a small but just working local webserver/mysql/php stack), get live results. For serious PHP programming (yes, not everyone acknoledges this ^^) the free Netbeans for PHP is a nice tool. I am looking forward to see the next releases. Till then I will use it for a while and hope that it will become simpler to use in the future without all the bloat :).

There is also a PHP plugin available for integration with Eclipse (this is also official supported by Zend) but because I never were the typical Eclipse guy (don’t know, just not the way I like things to look / be done), I did not test this in much detail. As far as I read the Eclipse PDT will integrate somewhat deeper with PHP thus featuring a debugger and easy integration with the Zend Framework. As I’m still a Notepad-Era guy running his own framework code, this may not be exactly what I am looking for (I also complained about the somewhat forced appserver integration in netbeans which is quite the same, you remember). However, if you plan to use most of the features of a complete IDE supported by the Zend guys, Eclipse PDT will be your way to go currently.

<?php die(”So far…”); ?>

Not long ago, Sun released a bundled package of their “Sun Java System Application Server” (Glassfish) and MySQL. I took this as the reason to try out PHP on Glassfish with enabled MySQL support. The initial installation was quite simple but there were some problems with documentation about the usage of MySQL. For those of you who already have a MySQL server running and just want ot test your application on Glassfish, the installation basically is:

  1. Download Glassfish/MySQL Bundle
    Or if you already have glassfish, add the Mysql Connector/J (the jar from the archive) to GLASSFISH/lib
  2. Download Quercus (most recent) and unpack the following files from the archives:
    quercus.jar
    resin-util.jar           to       GLASSFISH/domains/domain1/lib
    script-10.jar
  3. Add the following to GLASSFISH/domains/domain1/config/default-web.xml:
      <servlet>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <init-param>
          <param-name>script-encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
          <param-name>database</param-name>
          <param-value>ExampleMySQL</param-value><!– See below –>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <url-pattern>*.php</url-pattern>
        <init>
          <!– <php-ini>
            <sendmail_from>my_email_address</sendmail_from>
            <smtp_username>my_email_username</smtp_username>
            <smtp_password>my_email_password</smtp_password>
          </php-ini> –>
        </init>
      </servlet-mapping>
  4. Launch Glassfish and log in to the administration interface (by default: http://localhost:4848)
    Howto: Glassfish Quickstart Quide
  5. On Resources / JDBC / … in the administration interface add a new connection pool of type MySQL and set User/Password/Port/URI etc. in its properties (look through them when creating and don’t leave them blank). When done, “ping” (the button) your MySQL server from the connection pool view to test if it worked.
    Also add a new “JDBC Resource” that links to the pool and give it a name, in this case ExampleMySQL.
  6. Add a PHP file of your choice to GLASSFISH/domains/domain1/docroot and maybe restart Glassfish once to be sure the database changes are live.
  7. Run your MySQL-enabled PHP script (e.g. http://localhost:8080/index.php).

That’s basically everything. It took me some time to find out about the correct MySQL configuration (it seems that other tutorials I discovered at Google are quite outdated). Now, go and have fun!

Errrm: There are also enterprise clustering features implemented into Glassfish. I am wondering if this can scale a huge PHP site as well as lighttpd/php-fcgi. If anyone knows more details about this, please let me know. And mh: Just a thought: What about Glassfish + Quercus + MySQL + GridGain to scale at least the Webserver part into new dimensions? ;-)