From Java to PHP

Several months ago I accepted a new position, moving from an Application Developer to a Quality Assurance Supervisor. Prior to this move, I would never have considered moving over to QA, but thankfully, I still get to write code and writing code is really what I prefer to do.

One other big change is the switch from using Java for the past six years to using PHP. Most of the applications in my new position utilize PHP, and I had seen most of the jokes on Reddit regarding how bad it was, so I was skeptical. Now, I’m not fully convinced it’s something I would choose to use, given the option, but it’s not really as bad as portrayed. I like to think of it as a mix between Java and Perl, with a few of its own quirks thrown in for good measure.

I decided to write this blog post to help anyone who might be moving from using Java to needing to use PHP, to cover some of the basics of how to transition. This list is in no particular order, though I did try to cover the basics before diving into more nuanced topics.

With no further falderal, let’s get to the list!


I expect a lot from my IDE – code completion, class linkage, build tools, etc. For Java, I normally use IntelliJ IDE Community Edition. JetBrains (the makers of IntelliJ) do make a PHP IDE (PHPStorm), but unfortunately, it’s not free. If you can’t get your hands on PHPStorm, Visual Studio Code is a decent, albeit flawed choice. I haven’t yet found a good way to perform refactoring across classes, for instance.

Classpaths and packages

PHP has the concept of classpaths just as Java does, though it handles them differently and calls them namespaces. The easiest way I’ve found of handling this is to use composer. This requires just a single package.json in your main directory that lists both your source folders and namespaces as well as the external packages you want to include.


Class construction is fairly similar between Java and PHP with the same modifier concepts (public, protected, private) and use of methods. It also allows static member variables and methods, inheritance, interfaces, and abstract classes.

Member Variables

Member variables exist in PHP as they do in Java, with a caveat: you can’t declare a default that isn’t constant.

// valid
private string $whatever = "this';

// invalid
private string $whatever = aClass::staticMethod(); 

You also don’t need to set the type of the variable as you do in Java – PHP is not strictly typed.

private $whatever = "this"; // also valid

You don’t even need to declare a member variable to use it, though I wouldn’t recommend this approach as it can cause confusion and won’t be traceable by the IDE.


While the way functions and methods are used in PHP are similar to how Java uses them, their construction is somewhat different.

// Java
private void doThings(String $whatever) { 

// PHP
private function doThings(string $whatever) : void {

As above, the types of the parameters as well as the return types are not strictly required. Methods can even return different types. This will affect your IDE autocompletion, though, so use at your own risk

// We don't have to declare the parameter type
// or even the return type

private function doThings($whatever) {
  // We can return different types!
  return $whatever === "this" ? "Yes" : false;

And just a small change in how we actually call these functions:

// Java
MyObject o = new MyObject();
o.doThings();        // instance method
MyObject.doThings(); // static method 

// PHP
$o = new MyObject();
$o->doThings();       // instance method
MyObject::doThings(); // static method

PHP also has two other ways to call static methods when called from the same class:

class MyObject {
  public static function doThings() { }
  public function otherThings() {

The difference in the two comes up when inheritance is involved.

class MyObject {
  public static function doThings() { 
    echo "I am MyObject"

  public function otherThingsSelf() {

  public function otherThingsStatic() {

If we then define a child class…

class MyChildObject extends MyObject {
  public static function doThings() {
    echo "I am MyChildObject"

and then call the two functions on the MyChildObject, we see static introduces a concept of late static binding.

// Prints "I am MyObject"

// Prints "I am MyChildObject"


Other differences do exist in PHP, but the list above should be enough to grasp most of the concepts around OOP. If there are other things that you would like to see, please feel free to leave a comment!