Loading...

Hi, I'mVinay Lal

Web Developer
and Content Creator

Silverstripe and PHP7: faster websites and better coding

php7

Silverstripe 4 is the first version to use PHP7. I was doing some extra study on how PHP7, through its architecture, was made to be faster and I discovered that the speed of the PHP script execution was not the only benefit to Silverstripe using PHP7 as I had originally thought. It turns out there are a few more nuggets in there that can be useful such as new operators and type declarations. In this article, I look at two things that are useful from the new features.

Speed improvements:


The authors of PHP gained these speed improvements by changing the underlying architecture of PHP. This means that the PHP script processing portion of your website's loading speed will be faster. Many reports (using common software like WordPress) have reported a doubling of speed.

NOTE: This doesn’t mean your website as a whole will load twice as fast. It means that the PHP script portion of your load time will load twice as fast. It should be noted that the speed improvement won’t always be twice as fast. It could be less or more for your particular site but rest assured it should be faster in general.

Silverstripe is reporting speed increases of between 2-5 times faster for a vanilla Silverstripe project using PHP7 - their article here describing their testing and results.

If you are interested in the specifics of how the authors of PHP were able to gain such speed improvements then these posts goes into the nitty-gritty of PHP7’s architecture that made it so much faster.

I spoke to two experienced Silverstripe developers and they purely thought PHP7 was only useful for its underlying speed improvements and while this is mostly true there are a couple of new features which I have come to see as useful. The below isn’t an exhaustive list of new features by any means. I will link to a good video series and an article with further information if you want a more full understanding of all of the new features of PHP7 - see resources section below.

Null Coalescing Operator:

What is the Null Coalesce Operator?

In a nutshell its an operator which will simplify how you assign values to variables. It will allow you to assign a value to a variable if another is not present. It also allows you to do this on one line which means no if statements which could save you writing more code in some cases. The Null Coalesce Operator is made by writing two question marks like this, ‘??’ but we’ll go through some examples below.

Examples of the Null Coalesce Operator?

Let’s say you have a form which has some required and optional information so that when it submits sometimes you would have certain values to pass on and other times you didn’t. Assuming for some reason that you did have to set these optional fields in order to submit the form then the Null Coalesce Operator could be useful.

Let’s say the ‘Message’ field of a form is optional. We could set it like this with Null Coalesce:

  • $Message = $data[‘Message’] ?? ’No Message Given’;

That's fine you say but can’t we use a Ternary Statement to do the same? Well yes, you can but what if you needed to assign various data in case multiple variables weren’t populated? Well, in that case, the Null Coalesce Operator can deal with checking multiple variables.

  • $Message = $data[‘Message’] ?? $data[’AreaOfInterest’] ?? ’No Message Given’;

Why are they useful to incorporate into my coding?

It’s not a game changer but is a nice little tweak to my coding and nice to know that I can use something like this to assign variables values when there are complicated circumstances around doing so. The above example is for illustration purposes only. It’s not a real-life problem I’ve encountered. Form submissions may very well be an area of implementation though. The main point is as I said above, it’s good to know that you have the ability to write less code when assigning values is complicated.

Type Declarations:

What are type declarations?

Basically, a type declaration is a way to tell PHP what type a variable is. PHP is a weakly typed language meaning that PHP will allow you to use variables with unspecified types as it can set these automatically if you don’t specifics them yourself.

So if we don’t have to set them then why are type declarations useful? Well, one thing that leaving variables unspecified does is that it leaves our functions less readable. Often in PHP, it’s common to create a comment that goes with a function showing you what types the arguments and return values are. In PHP7 we can write functions which incorporate this information and ‘lock’ them into place so to speak.

On top of readability of code type declarations also make my applications and functions within them more robust i.e. less likely to be used incorrectly which is nice from an error point of view.

Example:

So as an example I can declare a function like this,

  • public function Example: int (string $argument1, int $argument2, bool $argument3, float $argument4){
        // do something useful with these inputs
        // then return some value
        return $output1;
    }

Here we are saying that this function must return an int (see the ":int" above) so $output1 needs to be an int. PHP7 will throw an error if this is a string. You can also declare a return type to be an object which may be useful as well ensuring that what is returned is an object of a particular type.

Resources:

 

How have you found PHP more useful? Share in the comments below.