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:

<?php

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:

/*
==RESULT== 
array(3) {
  [0]=>
  object(Car)#2 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    string(10) "Alfa Romeo"
  }
  [1]=>
  object(Car)#1 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    string(3) "BMW"
  }
  [2]=>
  object(Car)#3 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    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.

PhpStorm Tips #1 – Automatically declare a class field

Everyone should know how to get the most out of his working tool. To me, it’s the editor I use to code. That’s not the case for many of my fellow programmers, who know just the basic stuff.

That’s why I’ve decided to realize these little tips. Here’s the first one, on how to automatically declare a class field.

I hope you enjoy the show.