PHP and Java are both languages with reputations for getting things done, and there are plenty of reasons for combining the two, particularly in an Enterprise environment. The most common situation is for an organisation with an existing Java infrastructure to want to develop PHP projects, for all the reasons any enterprise would choose PHP, such as speed of development, cost, desire to use specific open source applications, or availability of staff. Often this is inspired by an internal group who has started using Drupal, MediaWiki, Wordpress or similar to great effect, but without the integration to the rest of the organisation's infrastructure that would be needed for a full scale roll out.
There are many reasons to want to integrate these new developments with Java rather than just creating a separate silo - existing libraries or systems that would be difficult or expensive to replicate, well-tested systems providing key functionality, and an existing team of developers are just a few. So, the best way is often to mix and match the two - which is easier than it might first appear.
Integration
There are four main ways of integrating PHP and Java, each with their own benefits and difficulties.
Indirect Integration
The simplest, though least architecturally clean, option is to simply have PHP and Java share a data source. Both languages can access every major database provider, so a common integration might be via a database. An example would be a Java system that provides user management storing data to an Oracle database, which is then read for a custom site login by a PHP application. This has the benefit of being straightforward, but introduces a dependency between both the projects and the database, which means changing both should a schema change be required, and making access restrictions more complex by requiring them to be duplicated across the two systems, or implemented purely in the database.
PHP on the JVM
Projects such as Quercus and IBM's Project Zero offer implementations of PHP that execute directly on the Java Virtual Machine. This allows PHP code to run alongside, and access, existing Java code. This can be perfect when there is a large existing Java infrastructure, or when PHP scripts need access to Java libraries or functionality directly. The downside of this integration is that the JVM implementations of the PHP interpreter don't always contain every feature in the latest version of the regular interpreter, so some applications may need modification in order to run properly.
For a further discussion on this option, see the InfoQ interview with the Quercus developers.
PHP to Java
In this case, a bridge is used to allow communication between PHP and native Java code. The bridge provides an integration point where PHP can include Java libraries and pass data back and forth. The advantage over the JVM projects is that PHP is still executing on the main PHP interpreter, so it's a more painless integration for a running PHP system that needs direct access to Java objects. The two main options are the open source PHP Java Bridge, and the Java Bridge built into Zend Server, which is proprietary but available freely as part of the Zend Server community edition.
For more on the Zend Java Bridge, see this presentation from Kevin Schroeder.
PHP consuming Java services
The fourth option is to expose the Java functionality needed as web services, using a common style such as SOAP or REST. This is the option implemented at several large enterprises where the Java services provide data access and processing, and are consumed by a PHP presentation layer which handles user interaction and front-end display. This service-oriented architecture takes advantage of the best of both languages, and allows scale out of individual components as well as excellent division of responsibilities. The disadvantage is that this requires work on both sides to make the services available, and to consume them. However, if the organisation already has a set of services as part of an existing architecture, it is usually straightforward to consume them on the PHP side.
For more information about SOAs and services in general, download the Ibuildings white paper on Service APIs.
Working Together
PHP and Java are very different languages, with different histories and features. However, both are well suited to the problems of enterprise web development, as both are perfect for pragmatic, effective solutions. Maintaining a multiple language infrastructure, particularly as part of a service-oriented architecture, allows developments in either space to benefit the organisation, and for projects to be built with whichever tool fits the requirements and resources best.
If you are interested in exploring how PHP could add value to your existing Java infrastructure, fill in our contact form.