Skip to content

Instantly share code, notes, and snippets.

@cspray
Last active July 5, 2023 12:29
Show Gist options
  • Save cspray/5651338 to your computer and use it in GitHub Desktop.
Save cspray/5651338 to your computer and use it in GitHub Desktop.
A list of differences I've noticed between PHP and Java.

Java vs. PHP

As is pretty obvious by my blog, GitHub repos and my job I'm a PHP developer. I'm also pretty adept with JavaScript and SQL. Lately I've been getting back into Java development a little more seriously. This gist is a series of short articles about some of the differences I've noticed between PHP and Java.

To be specific my Java development recently has been web development using Google App Engine. I am intending to create a RESTful API utilizing the language.

The differences discussed are not intended to be an indictment against either language. Some things about PHP I really like, some things about PHP I really dislike. The same goes for Java. Some things PHP is really good for and other things Java is really good for. This is not meant to be a religious war between languages.

I realize that in a way this is comparing apples and oranges. PHP is a dynamic, interpreted (althought yes it is still compiled) scripting language while Java is a static, compiled (although yes it is still interpreted) programming language. We aren't here to discuss just basic differences; we want to look at distinct ways the languages differ that encourages thoughts and insights into why the two languages were designed the way they were.

Constructor Inheritance

In PHP constructors are "completely" inherited while in Java constructors are only "partially" inherited. Let's take a look at a code example to tell the differences.

First one in PHP.

<?php

abstract class AbstractObject {
   
    protected $foo;
    
    public function __construct($bar) {
        $this->foo = $bar;
    }

}

class Concrete extends AbstractObject {}

$Concrete = new Concrete('bar');

In PHP this is a completely valid call. If you interact with $foo within the object's scope it will be assigned 'bar' for the specific instance in the example. Let's take a look at an equivalent example in Java.

abstract public class AbstractObject {

   protected String foo;

   public AbstractObject(String bar) {
      this.foo = bar;
   }

}

public class Concrete extends AbstractObject {}

Concrete obj = new Concrete("bar");

This is not valid Java code and will fail on compilation. While Concrete can call the constructors in AbstractObject it does not truly inherit them. Calls to parent constructors must be explicitly propagated by the extending classes.

My thoughts

I am not a big fan of the way Java handles this use case. It seems like it would lead to a lot of what I call "tedious code". Code that's there just to call another system for no discernible reason or gain. The fix, of course, for the above Java conundrum is to change your Concrete class to look like:

public class Concrete extends AbstractObject {

   public Concrete(String bar) {
      super(bar);
   }
}

This, in my opinion, is the exact definition of tedious code. We're forced to write those lines for no other reason than to pass it on to the parent class. Now, I realize that Java is much more object oriented than PHP is and I'm only just getting into the language. But I don't quite understand the design decision here. What were the reasons for designing constructor inheritance in this way?

Single Quotes

This one is kind of a no-brainer and really just more muscle memory that one establishes when working with different languages. I'm use to single quotes in PHP. I realize that in modern PHP versions quotes don't really make that much of a performance difference but single quotes is what I use and it's pretty consistent.

Java quotes make a lot more difference than PHP. Single quotes is only for char types. String types need the good ol ". Many backspaces have been hit because of this. Eventually I might be able to seamlessly swap between the two...but I doubt it.

Dynamic Method Calling

A pretty common use case in PHP for dynamic method calling is the use of the pretty URL /controller/action/param routing pattern. Let's take a look at a Controller with multiple actions and calling them dynamically in PHP.

<?php

class Controller {

    public function index() {
        // do stuff for / home page
    }

    public function about() {
        // do stuff for /about page
    }

}

// let's take a look at how we would invoke these 2 functions dynamically
// based off a pretty URL convention

$indexUrl = '/controller/index';
$aboutUrl = '/controller/about';

list($controller, $action) = getControllerParts($indexUrl);
$controller->$action();

list($controller, $action) = getControllerParts($aboutUrl);
$controller->$action();

As you can see calling methods dynamically in PHP is pretty straight forward. You populate a variable with the method you want to call and you, well, just call it; like you would for any other method call. This is simple, easy and intuitive in PHP and opens up this kind of functionality to be abstracted away and easily duplicated.

Now, let's take a look at a similar example in Java.

public class Controller {

    public void index() {
    
    }
    
    public void about() {
    
    }

}

String indexUrl = "/controller/index";
String aboutUrl = "/controller/about";

ControllerParts indexParts = ControllerParser.getParts(indexUrl);
ControllerParts aboutParts = ControllerParser.getParts(aboutUrl);

Controller controller = ControllerFactory.createController(indexParts.controller());
Method method = controller.class.getMethod(indexParts.action());
method.invoke(controller);

Controller controller = ControllerFactory.createController(aboutParts.action());
Method method = controller.class.getMethod(aboutParts.action());
method.invoke(controller);

So ultimately the Java code can be abstracted away so that a lot of this duplication isn't present. But, you still require Reflection to utilize this functionality. Perhaps at lower levels PHP dynamically calling methods and Java calling them via Reflection are similar process wise and maybe they aren't. Ultimately though the calling code in Java has to take more steps to dynamically call methods.

Java closer to the metal. PHP closer to the web.

It is indisputable fact that Java is exponentially more performant than PHP in virtually everything you can do with either of the languages.

@xeoncross
Copy link

You might want to look at Go as well. Go is closer to the metal and the web since the whole stack can be Go (the database, server, and app). The main thing PHP devs might miss are more mature ORM's and sorted maps (or in PHP they are called associative arrays).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment