Trying out PHP after 7 years

 Introduction

 

This post will be weird for all of my usual readers. I'm a Lead Java Developer that's been writing about fun and clever things to do in Java ecosystem. What most of you probably don't know is that prior to working in Java, I was a Lead PHP Developer for quite a while. Yup. The PHP. The language that is so divisive, that on one hand, drives more than 75% of the entire Internet and, on the other hand, is considered by many to be a pathetic, ugly scripting language, no better than universally hated JavaScript.

 

The Premise

7 years ago, when I was finally parting ways with PHP to focus solely on Java, I was among the PHP haters. I was fed up with weird syntax and Object Model, inconsistent naming conventions, no static typing of any kind, structural programming legacy all around the place, scripting nature that meant having to execute everything from scratch (even if cached) for every Request, slow execution and the need of caching literally everything to be able to achieve a good performance. 

Prior to joining my current employer I've had lots of experience with Java for both Desktop and Web. I was a Certified Oracle Developer with OCPJP6 and OCEJWCD6 certs to prove it. PHP was, however, the central part of my day-to-day work. I was eager to abandon it and focus my career on Java. It seemed so much better. It had all the things I wanted to use and that PHP didn't have. Even though PHP in the early 2015 was already making much progress from the dark ages of its early, structural nature, I didn't want to wait any longer. 

Now, after 7 years of pure Java development I know that, in most cases, grass isn't always greener. Don't get me wrong - Java is great and I love it, but it has its fair share of skeletons in the closet, like: long compilation and deployment time (looking at you, Spring), funky generics, primitives/boxing conundrum, extremely high verbosity and boilerplate, too many dependency managers and build tools - each with its own quirks, etc. The conclusion is - no language/platform is perfect.

So, maybe it's time to give PHP another shot? It's been, after all, almost 7 long years. When I was getting out of the PHP ecosystem, PHP 5.3 (the one that introduced namespaces) was my target platform (although many have already adopted more modern 5.4 - 5.6 versions - either by just upgrading the runtime or actually applying the new & cool features), Symfony 2.6 was my Framework of choice (with 3.0 being just around the corner). Now, in the last quarter of 2021 we have PHP 8.1 and Symfony 6.0. Both with innumerable improvements and new features. 

 

Current State of PHP

PHP 8.1, compared to PHP 5.3 has (and I'm talking only about the most crucial differences):

  • completely new interpreter with JIT 
  • Object Model enhancements like  Traits, Enums, Constructor property promotion, read-only properties, Attributes (Annotations)
  • Full Static Typing (apart from local vars) with intersection Types, union Types and syntax-embedded null safety
  • Fibers (yup, co-routines are here)
  • Closures, including first-class Callable syntax

Additionally, PHP ecosystem has:

  • the best ever dependency manager  - Composer (Maven and Gradle can really go to hell, in comparison)
  • An absolute Developer Experience delight in the form of Symfony Framework - a thing that makes Spring Framework (which I love) look like a low level programming.
  • Tons of running options, including:
    • Roadrunner - Golang-backed server that makes PHP act almost like Java in regard to the load-once-run-multiple-times pattern
      • uses Goroutines for lightning performance
      • supports Go-written plug-ins for performance-heavy tasks
    • ReactPHP - an event-loop based Framework with reactive Database support!

 

Sure, it still has the baggage of all the previous PHP versions, but it has to be much nicer now. Why won't I give it a try and see for myself?

The Result

And so I did. The effect is my 2-part Article about re-creating the original Symfony Demo, but with all of my present Architecture experience and knowledge. During its creation I was able to remember most of the PHP knowledge buried deep in my brain, but also to try the new & cool features.

What is my overall feeling after spending some time with the new PHP and Symfony? To my surprise - the balance between PHP and Java has shifted to a place when both technologies have comparable number of major strengths and weaknesses:

  • PHP
    • Development Experience is much nicer in PHP due to the speed of the feedback-loop driven by Automated Tests
      • Java's Unit Tests, with all the needed compilation and build, can be slower than PHP's Integration Tests. PHP's Unit Tests give literally instantaneous feedback.
      • That of course will depend on the PHP version used and on the Framework of choice. Web development in plain PHP is not very popular. 
        • Plain Java Web (Jakarta) is at least equally awful.
        • There are some strong voices that promote using plain PHP due to PSRs being mature enough to be used on their own. While personally I'd choose a Framework every time, it's an interesting point of view.
    • The time needed to deploy a PHP App  (including Server Start) is an order of magnitude shorter than for a JVM-backed Application.
        • Example: in the time window needed to boot a single instance of Spring-backed webapp you can possibly spin dozens, if not hundreds, of PHP-based Pods.
        • Even though for a single instance, Java web app will still be considerably faster than PHP, the majority of the world has shifted into the distributed domain
        • This makes PHP a better choice for systems that need to be dynamically throttled in real time. Provided that resources are not an issue, distributed PHP app may even provide better response times due to it being instantaneously up-scaled by the auto-scaler.
    • Syntax, even though somewhat unusual and full of weird functions, is much less verbose and allows go get things done faster than in Java 
      • Kotlin fixes that immensely for the JVM
    • Object model enhancements like Traits, Closures and 1st class Callable make creating Business Logic interesting and fun
    • PHP libraries are not constrained by old, ridiculous APIs
      • Doctrine ORM is much more convenient to use than Hibernate, due to the latter being pervasively used via the horrific, outdated and full of bad design choices JPA API.
        • I don't hate Hibernate. It's pretty good on its own and its getting things done. But JPA, on the other hand, is a pile of old, not updated, atrocious, horrible APIs (Criteria API - I'm looking at you). Using Hibernate through JPA can be very painful.
  • Java / JVM
    • Raw performance (after warm-up) is still considerably better
    • Ability to use stateful, managed resources
      • Database Connection Pooling pretty much out of the box  (in PHP its strongly db-driver dependent and generally not available, but should be more widely accessible via Roadrunner in the near future)
    • True parallelism with Threads and Thread Pools 
    • Still much better Type System with Generics
      • better IDE support for Java
        •  PHP Storm is great, but it cannot infer everything from the funky PHP's Type System
    • Collections Framework with Streams support

 

Conclusion

PHP just another tool in the box. It does some things better than Java and falls short in other areas. It's modern and robust enough for developing enterprise-scale Applications. It has a great, thriving community, wonderful Tools and Frameworks (Composer, Packagist, Symfony, Laravel, Yii, Twig, Doctrine, Behat, ReactPHP, PHPUnit, PHPSpec to name only a few) and, over the past couple of years, has made huge progress in both performance and features.

It most certainly is not a tool that anyone should be ashamed of and doesn't deserve the still strong ridicule. The history of PHP is the one of the greatest redemption arcs in Software Development History. I wouldn't be surprised if, in couple of years, PHP will have much more cool features. There already are some great, actively discussed RFCs, like Operator Overloading, Sealed Classes, Algebraic Data Types or C#-style property accessors.

After all, if you'd tell the PHP community 7 years ago that PHP language will have all the features it has today, they would think that you're crazy.

Comments

  1. Take a look at https://www.swoole.co.uk/, faster then reacthphp and has been around way before then fibers and as fast (faster then nodejs) as golang.

    ReplyDelete
  2. Wow i have been around PHP for 5yrs now and am inspired by this article

    ReplyDelete
  3. Try PHP 8.1 with Laravel, you'll fall in love. Symfony is low-level compared to Laravel. :)

    ReplyDelete
    Replies
    1. You mean the Laravel build with symfony components? I don't know another Laravel but that...

      Delete
    2. That's true, Laravel is build with Symfony components, but that's not a bad thing. Laravel has much more features and much faster and better ORM build in. I've been using both for the last 3 years and can say that Laravel lately has improved so much that Symfony next to it seems like it is low-level.

      Delete
  4. There is a duplication link in Roadrunner url

    ReplyDelete
  5. I recommend you also to explore the world of static analysis in PHP, with PHPStan and PSalm. It can be a game changing, for example adding Generics and other features compatible with PHPStorm.

    ReplyDelete
  6. Hey, did u checkout static analysers like PsalmPhp or PhpStan, they make generics possible and other useful static typing (at least at compile time). Types is a "compile time concept" anyway.

    ReplyDelete
  7. This post will be weird for all of my usual readers. I'm a Lead Java Developer that's been writing about fun and clever things to do in Java ecosystem. What most of you probably don't know is that prior to working in Java,

    I was a Lead PHP Developer for quite a while. Yup. The PHP. The language that is so divisive, that on one hand, drives more than 75% of the entire Internet and, on the other hand, is considered by many to be a pathetic, ugly scripting language, no better than universally hated JavaScript.

    Steinco Industrial Solutions, Inc.

    ReplyDelete
  8. There is also Laravel which makes coding with PHP even more enjoyable.

    ReplyDelete
  9. Try Quarkus. In developer mode you get hot reload and tests afffected by code changes are automatically run. You get faster feedback than with PHP. Use PHP for building monoliths Laravel with Livewire or InertiaJS or Symfony with Stimulus. But anything requiring a documented APIs other than CRUD should probably not be built using PHP.

    ReplyDelete
  10. You are a best blogger I was read

    ReplyDelete
  11. I have 10 years in java spring and now I'm building app using hyperf (php) , the best framework I have seen in java world and php, it's like spring boot but more powerful

    ReplyDelete

Post a Comment