A PHP time-lapse: tools and resources for Modern PHP

PHP has come a long way since its humble beginnings as a simple scripting language and Modern PHP has several peculiar traits (see what I did there?)

But with a simple Google search along with great material about how to write Modern PHP you will find a lot of outdated blog posts or resources that should be ignored.

In this post, I’ve tried to collect a few outstanding resources that have helped me become a better PHP developer.

But first, the time-lapse.


I still remember the first website I created using PHP. It was during high school: I was teaching myself Turbo Pascal and I was able to do simple sites in HTML.

Then I saw a friend of mine programming, and it wasn’t Turbo Pascal. I remember that all those dollar signs were a bit strange.

What’s that? It’s a new programming language, it’s called PHP. You can build dynamic websites with it. What do you mean with dynamic?

Without talking, he wrote something like this:

echo '<ul>';
for($i=0; $i<1000; $i++){
    echo '<li>Item number ' . $i . '</li>';
}
echo '</ul>';

And reloaded the page (he was using EasyPHP as a local server).

One thousand li element created on the fly. ONE THOUSAND. It would have taken me a bit of time to write that in static HTML. I wanted, better, I needed to learn how to do that!

I copied the EasyPHP installer on some random floppy disk, along with some PHP files to learn from. That evening I wrote my first Hello, World and a great looking website that featured header, sidebar, main content, and footer all split into separate files and fetched via include. I felt powerful!


Fast forward five years


I’m at university and I find a little part-time job as a web developer. I’m excited about all the things I’m going to learn. They told me I need to migrate an app from PHP4 to PHP5. Of course, I wasn’t prepared for this:

//UPDATE LAST ACTION
$DB->Execute("UPDATE users SET last_action=NOW() WHERE id=".$_SESSION['fw_userid'] );

//CHECK FOR EXPIRED PASSWORD
if ($CONF['days_cpw'] > 0)
{
    list($yy,$mm,$dd) = explode("-",$_SESSION['user']['last_pw_change']);
    $olddata = mktime(2,0,0,$mm,$dd,$yy);
    $span = time() - $olddata;
    $days_passed = $span / (60*60*24);
    if ($days_passed > $CONF['days_cpw'] && !strstr($_SERVER[SCRIPT_NAME],"mod_user") && !strstr($_SERVER[SCRIPT_NAME],"calendar_iframe")  && !strstr($_SERVER[SCRIPT_NAME],"cron_web_start") )
    {
            header("location: ".$CONF['url_base'].$CONF['dir_modules'].'admin/pages/mod_user.php?id='.$_SESSION['fw_userid'].'&action=cp');
    }
}

//Template inizialization
require_once ("template.php");

// Load Modules configuration in memory
load_modules_config();

//Include permission manager
require_once ("permission.php");

Of course, PHP4 wasn’t the problem…It was a rough ride, but it was fun!


Fast forward fifteen years


I want to try out a new idea for a simple web app. I cd into a folder and create a PHP file.

Then php -S localhost:8000 -t public/ will spin off a dev server for me.

I need to read some CSV file: composer require league/csv is all I need to do to download a robust library for CSV handling.

Since I want to make sure that my application is robust, let’s install a testing framework: composer require --dev phpunit/phpunit ^8.

I don’t like to use require_once all over the place: luckily Composer provides a PSR-4 compliant autoloader for free.

Now let’s try to make an API for that: I can choose from multiple PSR-15 Server Request Handlers that digest PSR-7 HTTP messages and focus just on my business logic.


Tools & Resources

If you want to know more, here are several resources and tools that have helped me learn more about the language.

PHP: The Right Way

If you could choose just one resource to know more about Modern PHP, then PHP: The Right Way would be that one.

It’s less about the language and more about the ecosystem and the best practices that have emerged during the years.

An absolute must-read.

Dependency management via Composer

Composer logo
Composer logo

I can say without exaggeration that Composer is one of the best things happened to PHP. I always found PEAR confusing to use, while Composer is immediate and ubiquitous.

Of course, a dependency manager without packages to manage is a bit useless so I need to mention also Packagist, a repository where (almost) all PHP libraries are uploaded and fetched by Composer.

I think that Composer played a big part in the recent switch from monolithic framework to composing of libraries that

Some great reads about this topic:

Modern PHP

Modern PHP book cover
Modern PHP book cover

Even if this book by Josh Lockhart is a bit old, it does a great job at explaining “new” features introduced in PHP and other best practices. It’s a great complement to PHP: The Right Way.

Psysh

The one thing I missed from my times as a Python developer was the iPython REPL. After discovering Psysh, my nostalgia faded away.

It’s simply the best REPL for PHP, and its doc feature gives me the ability to check the PHPdoc without leaving the terminal every time.

The doc command for array_map
The doc command for array_map

Testing

A list like this wouldn’t be complete without mentioning some testing-related resource. But it will be a really obvious one.

I still feel that the best place to start is the PHPUnit Manual as far as PHP is concerned.

Then, if you want to learn more about testing in general and TDD, these are my top recommendations:

Modernizing Legacy Applications In PHP

Modernizing Legacy applications with PHP book cover
Modernizing Legacy applications with PHP book cover

We’ve all been there. During the interview, you’re sold futuristic technology and once you’re at your new job, you’re faced with a big, leaking bull of mud. And you just want to run away.

Modernizing Legacy Applications In PHP by Paul M. Jones helps you step by step untangle that mess. Really well written!.


So these are my favorite resources about Modern PHP: did I miss something? What’s yours?

One thought on “A PHP time-lapse: tools and resources for Modern PHP

  1. Hi there! I really enjoyed this reading. Maybe I don’t have all those nostalgic years behind me (because I’m not so old… joking) but the truth is you’re right when you point out all the evolution that we feel now using this great language.
    And I’m pretty sure the end is not even close yet, so there is too much to see in the near future and I can’t wait to be there…
    so cheers for the old and good times with our PHP and for those who are about to come.

Leave a Reply

Your email address will not be published. Required fields are marked *