The PHP 7 Story

I’ve been craving me some PHP lately, not PHP code per se – I code in PHP on the daily. PHP was my first programming language – I fell for PHP the moment I saw it’s power, its force.

So this post is titled “The PHP 7 Story”, it sounded so cool. I’d like to give ALL the credit to Zeev Suraski for inspiring me to write this post. Yeah I didn’t know who this dude was either. I mean you probably know who “Rasmus Lerdorf” is if you’ve been programming in PHP a long while. So if Rasmus is the god of PHP, Zeev would be the demi god of PHP. Zeev gave a speech in Barcelona back in 2015 – yeah I’m so late to the conference attending game. My friends tell me I should get out and travel, makes me feel like a sheltered programmer but being a programmer is unlike any other profession or craft. Programming is within itself a traveling experience – yeah even time travel, anything is possible in the virtual world. Whoa I just felt like Neo from the matrix.

Ok this post is really based on this video – Zeev Suraski – The PHP7 Story, hence the name. Sometimes when I explore or reflect on programming languages I like learning about who wrote it. I’ll probably create another post dedicated to Rasmus Lerdorf but for now it’s about “The PHP 7 Story” – yeah I know what you were thinking, “Star Wars”. Yeah, I know – EPIC.

PHP is a popular language

Yes, PHP is a popular language – it once powered MySpace (not sure if it still does), Wikipedia and Facebook. It powers CMS platforms – WordPress, SquareSpace (just a wild guess) and most of the websites we see today. If you’re a developer then you know about Laravel, Symfony and CodeIgniter and friends – oh yeah and Zend, that would’ve been a huge shame not to list Zend. These are some killer PHP Frameworks they would eat up most of the node frameworks for breakfast that exist today in my opinion. Sorry Angular, React and friends. Except for Vue.js – I kinda like you.

First some core notes from the video in timeline fashion:

PHP 3

  • Released in June 1998
  • Introduction of extensions
  • Although token cache was available, still neededed to manually loop through data (a lot of times) so this was very inefficient

PHP 4

  • Released May 2000
  • Modularity – introduced new plugins, debuggers and more importantly the abstraction of the web server layer. Support for additional web servers – Netscape, Fast CGI and Microsoft IIS.
  • Downward compatibility
  • Sessions
  • Introduction of the Zend engine – this was more efficient than PHP 3’s looping method. The Zend engine (1) Compiled code, (2)Converted code into in-memory execution code then (3) finally executed.

PHP 5

  • New “True” object model (yes!)
  • Destructors
  • Exception Handling (I need to use this more)
  • And if you have a super high appreciation for architecture like I do then these two features matter a lot:
    • Ben thankful its not slower
    • Seeds were sown

PHP 6

The premise behind PHP 6 is as follows:

PHP 6 = PHP 5 + Unicode

The Unicode Standard provides a unique number for every character, no matter what platform, device, application or language.

This was great it allowed for Unicode to behave like first class citizens without additional API support – I didn’t know this. PHP 6 was the version I was most familiar with, I probably wrote more code in this version more than any other version. Thank you Zeev for informing me about this. There were some major performance issues resulting from this Unicode upgrade. So this was all great but…

  • PHP 6 was a lot worse than PHP 5!?
  • Memory consumption was bad!?
  • PHP 6 What Happened – if you’re interested in diving in deeper about what happened to PHP 6.

So this keep ringing in my head…

Performance, performance, performance

Been coding in PHP 6 for a very long time – this makes me feel a little enlightened actually. I think I’ve switched over most of my projects to PHP 7 so I’m relieved about that. If you haven’t switched over – yeah switch over ASAP.

Ok so fast forward to PHP 7…

PHP 7

So here’s the PHP 7 story…

  • Research PHP + JIT begins led by Dmitry Stogov – JIT did well on synthetic benchmarks
  • The result – no performance gains realized for rea world applications. Why? Memory consumption
  • Back to the drawing board, the PHP team got some help from Intel, yeah CPU processing was getting hit hard with JIT. Had to do away with JIT.
  • So the main goal with PHP 7 was performance – no new features, no nothing.

Birth of PHPNG – PHP Next Generation

  • Alternative PHP Cache (APC)
  • Zend OPCache
  • PHP still not JIT

I had to actually take a screenshot straight from the video itself for a better visual despite the crappy resolution.

Yeah major performance gains in PHP 7.

I was going to skip this part partly because this was more about my notes and thoughts about Zeev and the PHP 7 story. But I can’t help imagine how much hard work they put into PHP 7 in adding these new additional features I’ll list below. These guys nerded out, late nights, coffee, hack nights. Mad respect to your hustle guys and gals.

Engine Exceptions

Many E_ERRORS and E_RECOVERABLE_ERRORS are now exceptions.


try {

  call_method(null); // oops

} catch (EngineException $e) {

  echo "Exception: {
    $e->getMessage()
  }\n}
}

Return Type Declarations & Scalar Type Hints

Declare type of return value functions


<?php

function foobar(): int {

  return 1.0;

}

?>

New Scalar Type Hints: In two flavors


<?php

function add (int $a, int $b): int {
  
  return $a + $b;

}

?>

Zero Cost assert() (expectations)

Basically can configure PHP so that assertions don’t incur any penalties in both development and production. Assert is now a language level construct instead of just a function. Faster and easier to use.

Filtered Unserialize

Filter the instantiation of objects


// Supress all classes
$data = unserialize($foo, ["allowed_classes"=>false]);

// Allow only MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes => ["MyClass", "MyClass2"];

// Allow all classes
$data = unserialize($foo, ["allowed_classes"=>true]);

The Spaceship Operator


$x <=> $y

Uniform Variable Syntax


($foo->$bar)['baz']

The Null Coalesce operator


($x ?? $y) ~= ($x ? $x : $y) <----- ternary operator

To conclude Zeev makes a final statement that PHP 7 isn’t about 1 person or 50 people, PHP 7 is built atop a community. So keep coding, keep hacking.

Random thought – I’d really like to attend one of these conferences in Barcelona.