Lessons from php[tek] 2014

I recently had the opportunity to attend php[tek] 2014 in Chicago. As Tagged is a site primarily powered by PHP, it was a good opportunity to learn about upcoming features in unreleased versions of PHP, some of the newer features that are underutilized and not as well known, and just how far the language of PHP has evolved since it was released many many years ago.

New PHP features in 5.6

  • Constant scalar expressions
    • const ONE = 1; const TWO = 2; const THREE = ONE + TWO;
    • This allows much more flexibility in defining and re-using constants in your code.
  • Variadic parameters in functions
    • function f($required, …$params) { … }
    • This allows for much more flexibility in designing functions that can accept variable inputs, so the code is cleaner, more concise and more flexible.
  • Unpacking of arrays and traversable objects in functions
    • $operators = [1, 2]; echo add(…$operators);
    • This allows for more flexibility in calling functions, and thus eliminates having to use more boilerplate code.
  • Exponent operator
    • $four = 2 ** 2;
    • This eliminates some overhead and code cruft in calling the pow() function and the overhead of making a function call at all.
  • Use operator can import constants and functions now
    • use const Foo\const; use function Foo\func;
    • This allows for very easy access to possibly longer and messier function names or constants in addition to a much easier time reusing code.

Functional programming in PHP 5.x+

PHP has several new concepts that allow for much more flexibility and range of coding styles and practices, and one of the biggest is functional programming.

Functions can now be first class functions, which means they are first class citizens in the language. They can be declared as variables, passed into other functions, returned as a result of a function and stored in objects. This ends up leading to many other functional programming concepts that are available to the engineer working on a modern PHP app.

  • Closures
    • $greet = function($name) { echo “Hello $name!”; }; $greet(“Nick”);
    • Closures are very powerful constructs that are mostly useful in allowing functions to specify callbacks for much more customized and powerful behaviors.
    • Closures can also be passed around into functions and returned from functions.
  • Memoization
    • Memoization is a very powerful construct that allows you to cache the results of a function, drastically reducing the cost of calling any function that is an idempotent function, and can take much less time to execute.
    • Because of the availability of first class functions in PHP, it is easy to memoize almost any function you can write or access.
  • Currying
    • Currying is a transformation concept that means you can take one function that takes multiple inputs and break it down into a chain of multiple functions that take a subset (usually one less) of the parameters.
    • Currying is a power concept since it means you can take a more complicated function and reduce it down to other functions that have more of a restricted scope. This allows you to create a much more powerful expression from many smaller ones, and keep logic and scope sane and more well controlled.

PHP has come a long way from its humble roots almost 20 years ago as a templating language. It now has most of the comparable features that any other modern language has so you aren’t restricted to just one style of coding any more.

Nick Moore

Nick Moore is a Senior Software Engineer I on the web team at Tagged.

More Posts

Follow Me: