Upgrade to Pro — share decks privately, control downloads, hide ads and more …

New Wave PHP

Lorna Mitchell
September 09, 2014

New Wave PHP

The best of the new(ish) bits of PHP

Lorna Mitchell

September 09, 2014
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Anonymous Functions (5.3) Declare a function in the place where

    it is used. (you may have seen this in JavaScript)
  2. Anonymous Functions The old way: $things = array("name" => "Alice",

    "colour" => "Blue", "origin" => "Dreams"); function formatit($value, $key) { echo $key . ": " . $value . "\n"; } array_walk($things, "formatit");
  3. Anonymous Functions Using an anonymous function: $things = array("name" =>

    "Alice", "colour" => "Blue", "origin" => "Dreams"); array_walk($things, function ($value, $key) { echo $key . ": " . $value . "\n"; });
  4. Namespaces (5.3) Namespaces are for combining libraries in one project.

    • we keep our own code in its own namespace(s) • we can pick-and-mix other people's code • class names don't have crazy long prefixes
  5. Namespaced Library File namespace Lorna; class Nonsense { protected $words

    = array("wibble", "squeak", "howl", "pop"); public function speak() { return $this->words[ array_rand($this->words)]; } }
  6. Namespaced Calling Code include 'lorna/nonsense.php'; use Lorna\Nonsense; $nsense = new

    Nonsense(); echo $nsense->speak(); $other = new \StdClass();
  7. Array Dereferencing (5.4) function getList() { return ['Grumpy', 'Sleepy', 'Bashful',

    'Doc']; } // grab first item from list $item = getList()[0]; echo $item;
  8. Echo Shortcut <p>A template saying "hi, <?php echo $name ?>"</p>

    Could also be written as: <p>A template saying "hi, <?=$name ?>"</p>
  9. Short Array Notation (5.4) Three sets of equivalent notation: $game

    = array(0 => 'stone', 1 => 'paper', 2 => 'scissors'); $game[0] = 'paper'; $game[1] = 'scissors'; $game[2] = 'stone'; $game = [0 => 'scissors', 1 => 'stone', 2 => 'paper'];
  10. Less Nonsense (5.4) Removed in PHP 5.4: • register_globals •

    register_long_arrays • safe_mode • magic_quotes • allow_call_time_pass_reference • y2k_compliance • ereg* functions
  11. Easy Password Hashing (5.5) $pass = "secretpassword"; $hashed = password_hash($pass,

    PASSWORD_DEFAULT); echo $hashed; $2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSfflQtfjs
  12. Easy Password Hashing $existing_hash = '$2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C $pass = "secretpassword"; if(password_verify($pass,

    $existing_hash)) echo "All good"; else echo "Go away"; For PHP < 5.5: http://github.com/ircmaxell/password_compat
  13. Built-in OpCache (5.5) Replaces APC - beware this is disabled

    by default. Turn on opcache.enable and opcache.enable_cli
  14. How To Upgrade 1. Turn on E_DEPRECATED on your current

    platform and watch the logs (PHP 5.3+) 2. Compile PHP and run test suite with target version 3. Run application with PHP's webserver (PHP 5.4+) 4. Upgrade a test/staging platform 5. Go for it
  15. Finding Platforms New projects should be PHP 5.5 or later.

    Questions to ask: • What versions of PHP are available? • Are backups included? • Which extensions are available, and can I add others? • Can I get support with my PHP setup?
  16. Unofficial List of Providers • Servergrove http://www.servergrove.com/ • Linode https://www.linode.com/

    • Digital Ocean https://www.digitalocean.com/ • Siteground http://www.siteground.com/ • Rackspace http://rackspace.com
  17. Come to the Future PHP now has: • even better

    features • great tools • improved performance • a rich future with tools like Drupal built on it