My 2 Cents Php + java/.net languages


Content Writer
There are two very interesting projects allowing fellow programmers to mix PHP with Java or .NET languages in the same project.

If you are proficient in one of the many languages targeting the Java VM or the .NET framework, you will find the code-reuse capabilities appealing.

Quercus, PHP for Java

Quercus is a free and open-source PHP 5 implementation written entirely in Java. It is released under the GPL license.

You can place your Java classes under your webapp's WEB-INF/classes directory, then include them with the import statement.

For instance, if you place your class at: WEB-INF/classes/webmasterserve/

You would use it like:

    // Import your custom class
    import webmasterserve.WmsClass;

    // Instantiate it
    $wms = new WmsClass();

    // Examine it

    // Invoke a method
    echo $wms->someMethod();
This enables your PHP site to re-use the plethora of already-programmed algorithms coded using JVM languages; including the big boost of re-using your own Java-based code if you are a Java-oriented person.

Some well-known JavaVM languages you can use along with PHP -thanks to Quercus- are: Java, Clojure, Groovy, Scala, Jruby, Jython, plus the whole range at Wikipedia's List of JVM languages.

Official Quercus website:

Interoperability docs here.

Phalanger, PHP for .NET

Phalanger is an open-source implementation of PHP written in the C# programming language, offering a seamless .NET integration. It is released under the Apache License.

Once you have Phallanger installed and working, you can reuse .NET assemblies in PHP by adding the dll/library assembly to your Phalanger program's configuration file ("add assembly" for those cached in the GAC, "add url" for current-directory ones).

Say you used C# or VB.NET to compile a dll named WebMasterServe.dll with the namespace "WebMasterServe" and the class "WmsClass". Something like:

using System;

namespace WebMasterServe
    public class WmsClass
        public void WmsMethod()
To use your dll without adding it to the GAC, you employ add url in your config file:

<add assembly="..." />
<add url="WebMasterServe.dll" />

And your PHP code would look like:

    // Instantiate your class
    $wms = new WebMasterServe\WmsClass();

    // Invoke its method
Phalanger is very versatile. It makes PHP a first-class citizen in the .NET world, so you can use PHP to achieve anything doable with other .NET languages. For instance, you could make desktop applications in PHP using Winforms if you so wish.

With this PHP implementation, you can enjoy interoperability with .NET assemblies from languages such as C#, VisualBasic.NET, C++/CLI, F#, J#, Eiffel as well as the “Iron” ones -IronRuby, IronPython, IronScheme- in addition to the other languages featured at Wikipedia's List of CLI languages.

Official Phalanger website:

Interoperability docs here.

One last thing to ponder: speed

Even if you don't reuse external Java/.NET libraries along with PHP, a positive side-effect from running PHP on the JVM or .NET virtual machines being the speed boost you can obtain.

Quercus mentions a nice 4x speed boost on Drupal.

Phalanger has been used to speed-up WordPress in quite an efficient way, as seen on

When your site reaches a certain size, every optimization count.

Last edited: