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

Modernize Your PHP Code

Modernize Your PHP Code

Anna Filina

June 10, 2015
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. foolab.ca | @foolabca
    Modernize Your PHP
    Code
    IPC, Berlin - June 10, 2015

    View full-size slide

  2. Anna Filina
    • Developer
    • Problem solver
    • Teacher
    • Advisor
    • FooLab + ConFoo
    2

    View full-size slide

  3. Objectives
    • Simplify code
    • Improve design
    • Increase maintainability
    • Make coding more pleasant
    3

    View full-size slide

  4. Namespaces
    // 5.3
    // Avoid name collisions between classes.
    $my_user = new MyProject\Entity\User();
    $lib_user = new SomeLibrary\Entity\User();
    // Alias to long names.
    use Company\Project\Framework\Entity as Entity
    $user = new Entity\User();
    // Try Composer!
    4

    View full-size slide

  5. Closures / Anonymous Functions
    // 5.3
    // Assign to a variable
    $filter_function = function($item) { ... };
    5

    View full-size slide

  6. Closures / Anonymous Functions
    // Callback functions
    $products = array(
    array("name" => "Skyrim", "price" => 30),
    array("name" => "Destiny", "price" => 50)
    );
    $results = array_filter($products, function($item)
    {
    return $item["price"] <= 40;
    });
    6

    View full-size slide

  7. Traits
    // 5.4
    // Import groups of properties and methods
    trait TextOverlay {
    function setText() { ... }
    function setFontSize() { ... }
    }
    trait ImageBackground {
    function setUrl() { ... }
    }
    class FacebookQuote {
    use TextOverlay;
    use ImageBackground;
    }
    7

    View full-size slide

  8. Finally
    // 5.5
    // Practical if you want to execute code whether
    there is or no exception.
    try {
    create_temp_file();
    write_to_temp_file();
    output_temp_file();
    }
    catch(SomeException $e) {
    echo "Some error";
    }
    finally {
    delete_temp_file();
    }
    8

    View full-size slide

  9. Short Array Syntax
    // 5.4
    // Hard to read a complex structure.
    $products = array(
    array(
    "name" => "Skyrim",
    "price" => 30,
    "requirements" => array(
    "os" => array("Win XP", "Win Vista", "Win 7"),
    "ram" => 2048
    )
    ),
    array(
    "name" => "Diablo 3",
    "price" => 50,
    "requirements" => array(
    "os" => array("Win XP", "Win Vista", "Win 7", "Win 8"),
    "ram" => 1024
    )
    )
    );
    9

    View full-size slide

  10. Short Array Syntax
    $products = [
    [
    "name" => "Skyrim",
    "price" => 30,
    "requirements" =>
    [
    "os" => [ "Win XP", "Win Vista", "Win 7" ],
    "ram" => 2048
    ]
    ],
    [
    "name" => "Diablo 3",
    "price" => 50,
    "requirements" =>
    [
    "os" => ["Win XP", "Win Vista", "Win 7", "Win 8"],
    "ram" => 1024
    ]
    ]
    ];
    10

    View full-size slide

  11. Password Functions
    // 5.5
    $pass = password_hash("mypass", PASSWORD_BCRYPT, [
    "cost" => 12,
    "salt" => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)
    ]);
    // Between .25 et .50 seconds
    if (password_verify($form_password, $db_password))
    {...}
    11

    View full-size slide

  12. Foreach With List
    // 5.5
    $shape = [
    [0, 0],
    [2, 4],
    [4, 0]
    ];
    foreach ($shape as list($x, $y)) {
    line_to_coords($x, $y);
    }
    12

    View full-size slide

  13. Argument Unpacking
    // 5.6
    function addToCart($qty, $name, $price) {...}
    $product = ["Civilization V", 40];
    addToCart(1, ...$product);
    // addToCart(1, $product[0], $product[1]);
    13

    View full-size slide

  14. Anna Filina
    • Development: PHP, JS, etc.
    • Fix problems: bugs, performance, etc.
    • Workshops: testing, Symfony, AngularJS, API, etc.
    • Advisor: testing strategy, legacy code, etc.
    14

    View full-size slide

  15. @afilina afilina.com

    View full-size slide