what's new in php 5.3: volume one

49
What’s New in PHP 5.3

Upload: david-zuelke

Post on 10-May-2015

3.988 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: What's New In PHP 5.3: Volume One

What’s New in PHP 5.3

Page 2: What's New In PHP 5.3: Volume One

Johannes Schlüter

Page 3: What's New In PHP 5.3: Volume One
Page 4: What's New In PHP 5.3: Volume One
Page 5: What's New In PHP 5.3: Volume One

David Zülke

Page 6: What's New In PHP 5.3: Volume One
Page 7: What's New In PHP 5.3: Volume One
Page 8: What's New In PHP 5.3: Volume One

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

Page 9: What's New In PHP 5.3: Volume One

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

1.000.000 1.500.000 2.000.000

75.000.000.000100.000.000.000

Woot Level

Page 10: What's New In PHP 5.3: Volume One

Volume One

Namespaces

Closures, Part One

Late Static Binding

Operators, Syntax, Magic Methods & Constants

New & Removed Extensions, BC

Windows Support

Volume Two

PHARClosures, Part TwoSPLStream ChangesDate and Time Handlingext/mysqlndext/intlgetopt, OpenSSL, php.iniPerformance/Internals/GC

Page 11: What's New In PHP 5.3: Volume One

What’s New in PHP 5.3Volume One

Page 12: What's New In PHP 5.3: Volume One

Name\SpacesBecause Underscores Are Not Enough

Page 13: What's New In PHP 5.3: Volume One

Namespaceable Elements

namespace Foo;const ANSWER = 42;class C { /* ... */ }function f() { }

echo Foo\ANSWER;new Foo\C();Foo\f();

Page 14: What's New In PHP 5.3: Volume One

Multiple Namespaces

namespace Foo {    class C { /* ... */ }}namespace Bar {    class C { /* ... */ }}

Page 15: What's New In PHP 5.3: Volume One

Namespaces Example #1

MyFramework/someModule/Foo.class.php:<?phpnamespace MyFramework\someModule;class Foo { /* ... */  }?>

app/Bar.class.php:<?phpclass Bar extends MyFramework\someModule\Foo { /* ... */  }?>

Page 16: What's New In PHP 5.3: Volume One

Namespaces Example #2

MyFramework/someModule/Foo.class.php:<?phpnamespace MyFramework\someModule;class Foo { /* ... */  }?>

MyFramework/someModule/Bar.class.php:<?phpnamespace MyFramework\someModule;class Bar extends Foo { /* ... */  }?>

Page 17: What's New In PHP 5.3: Volume One

Namespaces: call_user_func// it's a string, so we need to use a FQNcall_user_func_array(  array(    __NAMESPACE__.'\some_class',    'method'  ),  $params);

Page 18: What's New In PHP 5.3: Volume One

Class Resolutionnamespace MyFramework\someModule;class PdoException {}

new PdoException(); // MyFramework\myModulenew \PdoException(); // ext/pdo

new DateTime(); // class not found!new \DateTime(); // works

Page 19: What's New In PHP 5.3: Volume One

Function Resolutionnamespace MyFramework\someModule;function strlen($param) {    return 0;}

echo strlen("hello world"); // 0

echo \strlen("hello world"); // 11echo some\other\space\strlen("hello world");

Page 20: What's New In PHP 5.3: Volume One

Using Namespaces

foo/bar.php:<?phpnamespace foo\bar;class class1 {} // foo\bar\class1?>

some/code.php:<?phpuse foo\bar;new bar\class1();?>

Page 21: What's New In PHP 5.3: Volume One

Aliasingfoo/bar.php:<?phpnamespace foo\bar;class class1 {} // foo\bar\class1?>

some/code.php:<?phpuse foo\bar as baz;use foo\bar\class1 as zomg;new baz\class2();new zomg();?>

Page 22: What's New In PHP 5.3: Volume One

ClosuresChapter One: The Basics

Page 23: What's New In PHP 5.3: Volume One

Closures: Very Basic$array = array(3, 9, 2);

array_filter(  $array,  function($element) {    return $element > 5;   });

Page 24: What's New In PHP 5.3: Volume One

Late Static Bindingohai, I’m in ur base, calling teh selfz

Page 25: What's New In PHP 5.3: Volume One

The ActiveRecord example

$user = User::findByID(23);

echo "Hello " . $user‐>getName() . "!\n";

$user‐>setPassword("verysecret");$user‐>save();

Page 26: What's New In PHP 5.3: Volume One

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);

Page 27: What's New In PHP 5.3: Volume One

self vs static with LSBclass Base {  public static function m()  {    self::printName();    static::printName();  }

  static function printName()  {    echo __CLASS__;  }}

Base::m();

BaseBase

Page 28: What's New In PHP 5.3: Volume One

self vs static with LSB

BaseExtended

class Extended extends Base {  static function printName()  {    echo __CLASS__;  }}

Page 29: What's New In PHP 5.3: Volume One

Operators, Syntax, Magic Methods & ConstantsAnd Exceptions That Eat Each Other

Page 30: What's New In PHP 5.3: Volume One

__callStatic()

class Foo {    public static function __callStatic($name, $args) {        echo $name;    }}Foo::boats();

boats

Page 31: What's New In PHP 5.3: Volume One

Static Calls w/ $classname

$cls = 'DateTime';$cls::getLastErrors();

Page 32: What's New In PHP 5.3: Volume One

Constants in Global Scope

const EGG = 'bacon';

Page 33: What's New In PHP 5.3: Volume One

NOWDOC

echo <<<'BAI'Price: $US 375BAI;

Price: $US 375

Page 34: What's New In PHP 5.3: Volume One

HEREDOC

echo <<<"BAI"Price: $US 375BAI;

Notice: Undefined variable: US...

Page 35: What's New In PHP 5.3: Volume One

__DIR__

// old: function call, conditional includeinclude(dirname(__FILE__) . '/brother.php');

// new: magic constant, no conditional includeinclude(__DIR__ . '/brother.php');

Page 36: What's New In PHP 5.3: Volume One

Exception Linking

try {    $pdoStatement‐>execute();} catch(PDOException $e) {    $up = new RuntimeException('PDO fell over', 0, $e);    throw $up; // lame joke, ain't it?}

Page 37: What's New In PHP 5.3: Volume One

Ifsetor

$id = $_GET["id"] ?: 0;

Page 38: What's New In PHP 5.3: Volume One

Notice: Undefined index: id

Page 39: What's New In PHP 5.3: Volume One

goto

label: durr;$i++;if($i < 100) {  goto durr;}

Page 40: What's New In PHP 5.3: Volume One

New/Removed ExtensionsAnd Backwards Compatibility Changes

Page 41: What's New In PHP 5.3: Volume One

New Extensions

intl

sqlite3

mysqlnd

phar

fileinfo

enchant

Page 42: What's New In PHP 5.3: Volume One

Removed Extensions

msql

dbase

fbsql

fdf

ncurses

ming

sybase (-> sybase_ct)

Deprecated: ereg

Page 43: What's New In PHP 5.3: Volume One

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

Page 44: What's New In PHP 5.3: Volume One

call_user_func_array w/ refs

function foo(&$param) {}// breakscall_user_func_array("foo", array(23));// breakscall_user_func_array("foo", array($var));// workscall_user_func_array("foo", array(&$var));

Page 45: What's New In PHP 5.3: Volume One

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

Page 46: What's New In PHP 5.3: Volume One

WindowsMuch improved

Page 47: What's New In PHP 5.3: Volume One

BugfixesSome of which are really, really important

Page 48: What's New In PHP 5.3: Volume One

- Fixed bug #47757 (rename JPG to JPEG in phpinfo). (Pierre)

Page 49: What's New In PHP 5.3: Volume One

Thank You And Goodbye!Johannes Schlüter ([email protected])

http://twitter.com/phperror

David Zülke ([email protected])

http://twitter.com/dzuelke