A PHP time-lapse: tools and resources for Modern PHP
Tags: php, resources
Published: 2019-07-31 20:36:59

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:

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

And reloaded the page (he was using [EasyPHP](https://www.easyphp.org/) 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:

```php
//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](https://www.php-fig.org/psr/psr-4/) for free.

Now let's try to make an API for that: I can choose from multiple [PSR-15 Server Request Handlers](https://www.php-fig.org/psr/psr-15/) that digest [PSR-7 HTTP messages](https://www.php-fig.org/psr/psr-7/) 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](https://phptherightway.com/) 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](https://giustino.blog/wp-content/uploads/2019/07/logo-composer.png)

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](https://packagist.org/), 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 Without a Framework](https://kevinsmith.io/modern-php-without-a-framework) by [Kevin Smith](https://twitter.com/_kevinsmith)
* [Create your own PHP Framework](https://symfony.com/doc/current/create_framework/index.html) - Symfony docs


### Modern PHP

![Modern PHP book cover](https://giustino.blog/wp-content/uploads/2019/07/modern-php-cover.jpg)

Even if [this book](https://www.amazon.com/Modern-PHP-Features-Good-Practices/dp/1491905018) by [Josh Lockhart](https://twitter.com/codeguy) 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](https://psysh.org/), my nostalgia faded away.

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

![The doc command for array_map](https://giustino.blog/wp-content/uploads/2019/07/psysh-doc-command.png)

### 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](https://phpunit.readthedocs.io/en/8.2/) as far as PHP is concerned. 

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

* [Growing Object-Oriented Software Guided by Tests](http://www.growing-object-oriented-software.com/): one of best books about software design and TDD and tests in general,
* [Test-Driven Development By Example](https://www.amazon.it/Test-Driven-Development-Example-Kent-Beck/dp/0321146530): a must-read if you're serious about TDD,
* [The Way of the Web Tester](https://pragprog.com/book/jrtest/the-way-of-the-web-tester): a gentle introduction if you don't know anything about tests.


### Modernizing Legacy Applications In PHP

![Modernizing Legacy applications with PHP book cover](https://giustino.blog/wp-content/uploads/2019/07/modernizing-legacy-applications-php-book-cover.png)

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](https://leanpub.com/mlaphp) by [Paul M. Jones](http://paul-m-jones.com/) 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?