Tag Archives: Java

Tapping millions of non-PHP PHP developers. (A manager’s short guide for migrating from Java to PHP)

I was getting ready to post the Powerpoint presentations from my last two user group meetings when I fat-fingered my "My Documents" folder and accidentally opened up a Word doc that had an old article that I had written for a Zend newsletter a while ago.  So for this case of serendipity I decided that I would repost this article, which I had written long before I had a blog.  The basic premise is that once you get past a few minor differences, an organization can move from Java development to PHP development in a very short amount of time, saving time, money and headaches.

The Article

Every once in a while you are asked to write an article for a Zend newsletter. You come up with a couple of good ideas (all of which require a lot of work) and then one comes to you that is rather simple.  Then, to top it off, one of your customers sends you an email that confirms everything you were thinking. 

I have had the benefit of being able to program in many different languages for many different environments over the years.  Throughout that experience there are 2 languages that have risen to the top of my favorites list.  PHP is one, obviously.  But Java is the other.  Sometimes, though by no means always, there is some degree of derision towards Java in the PHP community among others. The usually points are made about Java performance, resource utilization and complexity.  And usually the points are true.  But Java, in my personal experience, is still an interesting language to write code in and it has an extremely diverse range of abilities that are not matched in most other languages.

But this is also a bit of a problem.  To access all of the functionality in Java, there is a tradeoff with simplicity.  To access all of the functionality for the web in Java, there is a massive tradeoff with simplicity.

In most cases, complexity costs more money and is more difficult to manage than simplicity.  That is not to say that complexity is a bad thing, or that maintaining software shouldn’t cost money.  But the more complexity you have in your application, the more complexity you have to maintain, which is in turn managed by complex tools that manage the complexity of your complex application.  This prompts me to ask 2 questions

  1. How much complexity (cost) your organization can absorb. 
  2. How much complexity (cost) your organization wants to absorb.

For a web-based environment, PHP is simply less complex and less costly than Java.  Java was never sold on the basis of cost of development and deployment, but in its write-once-run-anywhere nature and its vast array of abilities.  Moreso the latter.

At this point, you may be wondering how this fits in with the topic of non-PHP PHP developers.  And why should the manager of a Java shop even be considering PHP in the first place?  It is because if you have web-based applications and have Java developers, you already have PHP developers.  This is because there are a lot of similarities in terms of syntax between Java and PHP code.  Yes, there are differences, but not the sort of differences that stop your Java developers from writing PHP code.

This takes me to the customer whose comment I received.  The customer was a Java developer for 10 years, and in his words, took 2 days to get familiar with PHP.

2 days.

Let that sink in for a little.  In order for you to take advantage of the primary programming language of the World Wide Web, your Java developers could be writing usable PHP code in a matter of a few days (though probably a few more than just 2).  There are only a few important differences that your Java developers would need to take into consideration.  So let’s look at a few.

  1. The entry point is a script file, not a main(String[] args) definition or another kind of method call.  It is embedded in an HTML file, typically with the extension .php and starts with the delimiter
    <?php
    // Now I write code
  2. Variables start with a dollar sign.

    $ar = new ArrayObject();

  3. Variables are loosely typed, though not un-typed.

This means that unlike variable types can be acted upon as if they were like variable types.  For example, an array can be acted upon as a Boolean.

$ar = array(1,2,3,4,5);
if ($ar) {
            // The array was evaluated as a boolean
}

  1. Class methods are defined using the function keyword, not the return type.  There is no defined return type for PHP functions/methods.

    class User {
         public static function getUser($id) // no return type
         {
              return new User($id);
         }
    }

  2. Arrays are primitive PHP types which can contain ANY type of variable

You will typically not have different types of arrays like you would have LinkedList, ArrayList, etc.  An array (keyed or not) is an array.  However, if you wanted use arrays in an OO fashion you can use the ArrayObject class from the SPL library.

In terms of the bare minimum of differences, these 5 items are primary for Java developers.  There are definitely other differences, but to get started, this is virtually a complete list.  And it is reasonable to state that it is likely that your Java developers already know about points 1-3.  Most of the other rules of object oriented programming and web-based design that they have already been taught still apply.

Another way of saying it is that by moving to PHP, you retain all of the training and knowledge that you have invested in your developers while at the same time gaining the tremendous productivity and cost benefits of building your web-based applications in PHP. 

We can also take this a step further, which is the point of this entire article.  Say you have rebuilt your application in PHP and the CEO is inviting you to weekend trips to Pebble Beach because of all of the money you have saved the company and the reduced turn-around time of application development and deployment.  The CEO asks you to start moving other company applications onto PHP as well because it has been working so well.  Your peers may start questioning your decision to move to PHP because now you have to find more PHP developers.  But you have an edge.  You can hire any number of the millions of talented Java developers out there and, with a little guidance, have them writing PHP code in a matter of days.

Introduction to Caffeinated PHP – Using Java to extend PHP

If you are reading that title and thinking "what is that guy smoking?" then you are probably in good company.  Developers often make development decisions based off of philosophy.  Some developers go the route of "we will decide on the best tool (language) for this individual job" and end up having hundreds of individual tools that they need to end up supporting.  Others desire to take a more puritanical approach in saying that "we will only use this one tool (language) for this individual job."  The problem is that the decision is often not as cut and dry as many development philosophies allow.

In my experience, having a homogeneous environment is most desireable.  Limiting the number of languages and tools that you need to support is often a good thing.  The more tools you need to support, the less likely you are to be an expert in any of them.  Jobs whose posting states that the developer needs to know Java, PHP, ASP, Perl and C++ will likely end up getting someone who is not only decent in all of them, but lacking a depth of knowledge in all of them.  It's kind of like a job posting I saw for a C# developer that required 2 years of C# experience after C# had been out for 6 months.  I am mostly convinced that "using the best tool for the job" is the wrong approach.  What the better approach is "what is the best tool for solving the organization's problems".  Is PHP the best choice for a daemon?  Probably not.  But does your organization run PHP for your web properties and have minimal needs for some daemonized services?  Running PHP as a daemon may be a viable choice.  It's not "the best" choice for the job, but it might be the best choice for your organization.

But there are times when you have two disparate ecosystems that need to work together.  PHP is often the top contender for building out a web-based interface, but many organizations have internal systems that are running Java.  So do you have to squash your dreams of building your website in PHP because you need to have Java connectivity?  By no means!!

As part of Zend Server ( and CE) there is a feature called the Java Bridge.  Zend Server CE is our free version of Zend Server.  While Zend Server does have several useful "for-pay" features there are also several additional features that are shared between Zend Server and CE.  The Java Bridge is one of those features.  This means that you get the Java Bridge for free.

Using the Java Bridge is really quite simple.  First you need to log in to your instance of Zend Server and make sure that you have the Java Bridge installed.  You can see that we have it installed on the image on the right.  This means that both the Java Bridge extension and the Java Bridge daemon are up and running.  The daemon is basically a JVM that is running in the background and servicing requests made by the Java Bridge extension running in PHP.

If the Java Bridge has not been installed you can use the native installer.  In this case we are using yum.

[root@localhost ~]# yum search java-bridge
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirror.raystedman.net
 * base: mirror.san.fastserv.com
 * extras: mirror.skiplink.com
 * updates: mirrors.igsobe.com
============================= Matched: java-bridge =============================
php-5.2-java-bridge-zend-server.i386 : Zend Java bridge
php-5.3-java-bridge-zend-server.i386 : Zend Java bridge

We have the name, now we need to install it.

[root@localhost ~]# yum install php-5.3-java-bridge-zend-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirror.raystedman.net
 * base: mirror.san.fastserv.com
 * extras: mirror.skiplink.com
 * updates: mirrors.igsobe.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-5.3-java-bridge-zend-server.i386 0:3.1.26-2 set to be updated
--> Processing Dependency: java-daemon-zend-server for package: php-5.3-java-bridge-zend-server
--> Running transaction check
---> Package java-daemon-zend-server.noarch 0:3.1.25-15 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                          Arch    Version      Repository          Size
================================================================================
Installing:
 php-5.3-java-bridge-zend-server  i386    3.1.26-2     ZendServer          21 k
Installing for dependencies:
 java-daemon-zend-server          noarch  3.1.25-15    ZendServer-noarch   30 k

Transaction Summary
================================================================================
Install      2 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 51 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): php-5.3-java-bridge-zend-server-3.1.26-2.i386.rpm |  21 kB     00:00     
(2/2): java-daemon-zend-server-3.1.25-15.noarch.rpm      |  30 kB     00:00     
--------------------------------------------------------------------------------
Total                                            96 kB/s |  51 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : java-daemon-zend-server                                  1/2
To enable Java bridge, please run /usr/local/zend/bin/setup_jb.sh
  Installing     : php-5.3-java-bridge-zend-server                          2/2

Installed:
  php-5.3-java-bridge-zend-server.i386 0:3.1.26-2                               

Dependency Installed:
  java-daemon-zend-server.noarch 0:3.1.25-15                                    

Complete!

From there we need to set up the Java Bridge daemon.  Because there is an external dependency, namely Java, we need to do some basic setup on the Java Bridge.  Java may not be installed so you will need to make sure that you have that available.  If you have Java already installed you can run the zendctl.sh command, which will set up the Java Bridge daemon.  I have bolded my type.

[root@localhost bin]# /usr/local/zend/bin/zendctl.sh setup-jb
Welcome to Java bridge setup!
Please specify path the Java executable :
NOTE: please use Java by Sun or IBM version 1.5 and above.
/usr/bin/java
Starting Java bridge [OK]
[ 01.03.2010 16:23:58 SYSTEM] watchdog for java_daemon is not running.
[ 01.03.2010 16:23:58 SYSTEM] java_daemon is not running.
Stopping httpd:                                                      [  OK  ]
Starting httpd:                                                      [  OK  ]

At that point we're pretty much ready to go.  The Java Bridge works by initiating a TCP connection between the Apache process and the JVM.  The Java Bridge extension then sends command such as 'create object "System"' or 'run method "getProperties" on object "42fse2@System"'.  The Java Bridge then executes the request and returns the return value of that individual method call.  That return value can either be a regular Java object, a primitive or one of a few types that can be converted into native PHP types.  If you want to test your installation to make sure that it is running you can do so with the following code:

echo '

';

	$java = new java('java.lang.System');

	var_dump($java->getProperties());

If all is well and good you should get output similar to this:


array(54) {
  ["java.runtime.name"]=>
  string(28) "OpenJDK  Runtime Environment"
  ["sun.boot.library.path"]=>
  string(52) "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386"
  ["java.vm.version"]=>
  string(9) "1.6.0-b09"
  ["java.vm.vendor"]=>
  string(21) "Sun Microsystems Inc."
  ["java.vendor.url"]=>
  string(20) "http://java.sun.com/"
  ["path.separator"]=>
  string(1) ":"
  ["java.vm.name"]=>
  string(17) "OpenJDK Client VM"
  ["file.encoding.pkg"]=>
  string(6) "sun.io"
...

That's all for today.  We will look at some more practical examples in our next look at the Java Bridge.

Web Analytics