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.

Continue reading “A PHP time-lapse: tools and resources for Modern PHP”

Potential roadmap for multisite

Three plus years later, we’re still finding oddities in old MU code, so it makes sense to bide our time and only undergo renames when they let us shed dead weight. The side effect of keeping the barrier to entry high for multisite is not necessarily a bad one β€” even three years after merge, the product as a whole is still very weak (which is being kind).

Source: Potential roadmap for multisite: Subdirectories, subdomains, open registration, and domain mapping – Make WordPress Core

Sort an array by object attribute in PHP

One of those simple things that I keep forgetting.

Suppose you have to sort a set of PHP objects, based on an attribute.

To do this, you can implement the __toString method and use the SORT_STRING flag in the sort function:


class Car {
    private $_maker;

    public function __construct( $maker ) {
        $this->maker = $maker;

    public function __toString() {
        return $this->maker;

$car1 = new Car( 'BMW' );
$car2 = new Car( 'Alfa Romeo' );
$car3 = new Car( 'Volvo' );

$cars   = [];
$cars[] = $car1;
$cars[] = $car2;
$cars[] = $car3;

// sort the array
sort( $cars, SORT_STRING );

var_dump( $cars );

This will result in:

array(3) {
  object(Car)#2 (2) {
    string(10) "Alfa Romeo"
  object(Car)#1 (2) {
    string(3) "BMW"
  object(Car)#3 (2) {
    string(5) "Volvo"

How to debug with PHPStorm and PHP builtin server

I was playing with Symfony recently, and I needed to setup debug in PHPStorm using the PHP internal web server.

I’ve setup xdebug with PHPStorm in multiple environments, and each time I find a different glitch. Lucky me πŸ™‚

So, I will assume that you have PHP, xdebug, PHPStorm installed.

These are the steps:

  1. Edit the xdebug configuration file (in Ubuntu you can find it in /etc/php5/cli/conf.d/20-xdebug.ini) and add the following lines:
    xdebug.remote_autostart = 1
    xdebug.remote_enable = 1
  2. Start the server with php bin/console server:run
  3. In PHPStorm, click the phone handle icon.phpstorm_phone_handle_icon
  4. Set a breakpoint and load your application in the browser.

You’re done! Can you believe it?

This time the difference was that remote_autostart flag!

Until for next xdebug setup tip, bye πŸ˜‰

The importance of developing in a good environment

No, I’m not talking about that strange smell in your room.

This is a story that happened to me a while ago. It was certainly my fault, I could be more cautious, but in the end the net result was positive.

First job, first delivery, first meeting to check my work.
Imagine: I was awake all night studying the codebase in every detail, fixing all the warnings.
Refactoring, test, comments: you name it. I wanted to show that I was working good.

I arrived in time, turned on the PC to show off my work.
“No, let’s try it directly on the server!” – my boss said – “Have you updated the database?”
“Yes, I’ve added the necessary fields. Here’s the dump.”

Brilliant! Impeccable!

I upload the application. “Well, let’s try to add a record…” BAAAM.
A dreadful white screen with this error:

syntax error, unexpected T_OBJECT_OPERATOR in File.php on line 68

Panic. Alarm. Alarm. It couldn’t be. Obviously, it works on my machine.
I checked row 68, there was only a simple query:

$var = $DB->Execute($sqlQuery)->FetchRow();

What could be wrong with this row? I didn’t touch anything. A simple method chaining. Simple? Really?

How could I know that the simple method chaining wasn’t a core feature of PHP4? And I developed on PHP5 version. So, thank you, try better next time.

Luckily, the latest version was also installed on the server, so it was only a matter to switch.
But it could not be so. And I would have had to split all the lines like:

$result = $DB->Execute($sqlQuery);
$var = $result->FetchRow();

The moral of the story is: try to work as closely as possible to the production state. The results could be unpredictable.
And now, if you ask me, open your window and let the fresh air come in.