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

What's New In PHP 5.3: Volume One (IPC2009SE 20...

What's New In PHP 5.3: Volume One (IPC2009SE 2009-05-27)

Part one of the joint presentation with Johannes Schlüter on PHP 5.3, given at International PHP Conference Spring Edition in Berlin, Germany.

David Zuelke

May 27, 2009
Tweet

More Decks by David Zuelke

Other Decks in Programming

Transcript

  1. PHP Awesomeness History 0 25.000.000.000 50.000.000.000 75.000.000.000 100.000.000.000 PHP/FI PHP

    3 PHP 4 PHP 5 PHP 5.1 PHP 5.2 PHP 5.3 PHP 6 1 10.000 100.000 1.000.000 1.500.000 2.000.000 75.000.000.000 100.000.000.000 Woot Level
  2. PHP Awesomeness History, on a logarithmic scale 1 562 316.228

    177.827.941 100.000.000.000 PHP/FI PHP 3 PHP 4 PHP 5 PHP 5.1 PHP 5.2 PHP 5.3 PHP 6 1 10.000 100.000 1.000.000 1.500.000 2.000.000 75.000.000.000 100.000.000.000 Woot Level
  3. Volume One Namespaces Closures, Part One Late Static Binding Operators,

    Syntax, Magic Methods & Constants New & Removed Extensions, BC Windows Support Volume Two PHAR Closures, Part Two SPL Stream Changes Date and Time Handling ext/mysqlnd ext/intl getopt, OpenSSL, php.ini Performance/Internals/GC
  4. Namespaceable Elements namespace Foo; const ANSWER = 42; class C

    { /* ... */ } function f() { } echo Foo\ANSWER; new Foo\C(); Foo\f();
  5. Multiple Namespaces namespace Foo { class C { /* ...

    */ } } namespace Bar { class C { /* ... */ } }
  6. Namespaces Example #1 MyFramework/someModule/Foo.class.php: <?php namespace MyFramework\someModule; class Foo {

    /* ... */ } ?> app/Bar.class.php: <?php class Bar extends MyFramework\someModule\Foo { /* ... */ } ?>
  7. Namespaces Example #2 MyFramework/someModule/Foo.class.php: <?php namespace MyFramework\someModule; class Foo {

    /* ... */ } ?> MyFramework/someModule/Bar.class.php: <?php namespace MyFramework\someModule; class Bar extends Foo { /* ... */ } ?>
  8. Namespaces: call_user_func // it's a string, so we need to

    use a FQN call_user_func_array( array( __NAMESPACE__.'\some_class', 'method' ), $params );
  9. Class Resolution namespace MyFramework\someModule; class PdoException { } new PdoException();

    // MyFramework\myModule new \PdoException(); // ext/pdo new DateTime(); // class not found! new \DateTime(); // works
  10. Function Resolution namespace MyFramework\someModule; function strlen($param) { return 0; }

    echo strlen("hello world"); // 0 echo \strlen("hello world"); // 11 echo some\other\space\strlen("hello world");
  11. Using Namespaces foo/bar.php: <?php namespace foo\bar; class class1 {} //

    foo\bar\class1 ?> some/code.php: <?php use foo\bar; new bar\class1(); ?>
  12. Aliasing foo/bar.php: <?php namespace foo\bar; class class1 {} // foo\bar\class1

    ?> some/code.php: <?php use foo\bar as baz; use foo\bar\class1 as zomg; new baz\class2(); new zomg(); ?>
  13. Closures: Very Basic $array = array(3, 9, 2); array_filter( $array,

    function($element) { return $element > 5; } );
  14. The ActiveRecord example $user = User::findByID(23); echo "Hello " .

    $user‐>getName() . "!\n"; $user‐>setPassword("verysecret"); $user‐>save();
  15. Implementation (w/ Injection) abstract class ActiveRecord { static function findById($id)

    { $table= get_called_class(); $query = "SELECT * FROM " . $table . " WHERE id=" . $id; /* .... */ } } class User extends ActiveRecord {} class Entry extends ActiveRecord {} $a = User::findById(2); $b = Entry::findById(4);
  16. self vs static with LSB class Base { public static

    function m() { self::printName(); static::printName(); } static function printName() { echo __CLASS__; } } Base::m(); Base Base
  17. self vs static with LSB Base Extended class Extended extends

    Base { static function printName() { echo __CLASS__; } }
  18. __DIR__ // old: function call, conditional include include(dirname(__FILE__) . '/brother.php');

    // new: magic constant, no conditional include include(__DIR__ . '/brother.php');
  19. Exception Linking try { $pdoStatement‐>execute(); } catch(PDOException $e) { $up

    = new RuntimeException('PDO fell over', 0, $e); throw $up; // lame joke, ain't it? }
  20. Potential BC breaks New Keywords: namespace goto New global identifiers:

    New extensions New Closure class zend.ze1_comptibility_mode Parameter parsing: objects not interpreted as arrays anymore by these funcs: current, next, key, each, reset, end natsort, natcasesort, usort, uasort, uksort array_flip, array_unique
  21. call_user_func_array w/ refs function foo(&$param) {} // breaks call_user_func_array("foo", array(23));

    // breaks call_user_func_array("foo", array($var)); // works call_user_func_array("foo", array(&$var));
  22. E_DEPRECATED New error level to indicated deprecated stuff scheduled for

    removal in later releases Part of E_ALL which is good :) E_USER_DEPRECATED for userland errors Should be used for deprecated stuff, so E_STRICT can be used for the intended “bad practice” purpose