$30 off During Our Annual Pro Sale. View Details »

Get Ready for New PHP

Get Ready for New PHP

WordCamp London Talk

Lorna Mitchell

March 17, 2015
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Get Ready for PHP 5.4+
    Lorna Mitchell, WordCamp London

    View Slide

  2. Some Shiny, Some Gotchas

    View Slide

  3. Magic DIR Constant (5.3)
    Instead of:
    dirname(__FILE__);
    Now you can use:
    __DIR__;

    View Slide

  4. Anonymous Functions (5.3)
    Declare a function in the place where it is used.
    (you may have seen this in JavaScript)

    View Slide

  5. 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");

    View Slide

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

    View Slide

  7. 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
    • combined with PSR standards, they enable
    Composer

    View Slide

  8. Namespaced Library File
    namespace Lorna;
    class Nonsense {
    protected $words =
    array("wibble", "squeak", "howl", "pop");
    public function speak() {
    return $this->words[
    array_rand($this->words)];
    }
    }

    View Slide

  9. Namespaced Calling Code
    include 'lorna/nonsense.php';
    use Lorna\Nonsense;
    $nsense = new Nonsense();
    echo $nsense->speak();
    $other = new \StdClass();

    View Slide

  10. Register Globals is Gone (5.4)
    Upgrade PHP and see many undefined (simple)
    variables.
    To fix each undefined variable:
    • replace $username with $_GET['username']
    or $_POST['username'] in each case

    View Slide

  11. Echo Shortcut (5.4)
    PHP 5.4 removes short_open_tag config option
    • is never valid - s//• = is always valid
    = is the same as:

    View Slide

  12. 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

    View Slide

  13. Error Reporting: E_STRICT
    E_ALL includes E_STRICT in PHP 5.4+
    You can either:
    1. Fix your errors
    2. Turn it off again using E_ALL & ~E_STRICT

    View Slide

  14. Timezone Settings (5.4)
    PHP refuses to guess the timezone and will warn you.
    It is not safe to rely on the system's timezone settin
    You are *required* to use the date.timezone setting or
    date_default_timezone_set() function.
    In case you used any of those methods and you are stil
    warning, you most likely misspelled the timezone ident
    Fix it by setting date.timezone in php.ini:
    date.timezone = "Europe/London"

    View Slide

  15. Call Time Pass By Reference
    PHP Fatal error: Call-time pass-by-reference has been
    This error says "don't pass references in when the
    function didn't expect them, you fool"

    View Slide

  16. Call Time Pass By Reference
    You can pass by reference. Declare function:
    1 function inspire(&$person) {
    2 // be inspiring
    3 }
    Call with just $ and not &
    1 inspire($current_user);

    View Slide

  17. Easy Password Hashing (5.5)
    $pass = "secretpassword";
    $hashed = password_hash($pass, PASSWORD_DEFAULT);
    echo $hashed;
    $2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSfflQt
    fjs52GxhAc.s/G

    View Slide

  18. 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

    View Slide

  19. Built-in OpCache (5.5)
    Replaces APC - beware this is disabled by default.
    Turn on opcache.enable and opcache.enable_cli

    View Slide

  20. Get Ready for New PHP

    View Slide

  21. PHP Peformance

    View Slide

  22. Use PHPCodeSniffer
    Use the PHPCompatibility PHPCS standard
    Install from
    https://github.com/wimg/PHPCompatibility and
    include in your standards directory

    View Slide

  23. PHP Knows What's Deprecated
    Turn on E_DEPRECATED on an older version to see
    what's removed in the next version
    Set with: error_reporting = E_ALL

    View Slide

  24. Lint Check
    Syntax check your code with new version of PHP
    Use phing (http://www.phing.info/):






    Or try my one-liner:
    find . -name '*.php' | xargs -n1 php -l

    View Slide

  25. Serve with PHP
    Run application with PHP's webserver (5.4+)
    php -S localhost:8080

    View Slide

  26. PHP's Own Webserver
    php -S dev.project.local:8080
    -t /var/www/cool-project/web
    -c php-ini.development
    routing.php

    View Slide

  27. Use Continuous Integration
    Builds
    Use something like http://travis-ci.com to run regular
    builds
    This can run your build steps for various versions of
    PHP each time you commit
    (it's free for open source users)

    View Slide

  28. Check Your Code Is Ready
    • Use the PHPCompatibility PHPCS standard
    • Turn on E_DEPRECATED and watch the logs of
    your existing platform
    • Lint check with new version (php -l)
    • Run application with PHP's webserver

    View Slide

  29. Questions? (and resources)
    Intermediate PHP http://lrnja.net/php-video
    Contact me: @lornajane or http://lornajane.net

    View Slide