PHP 5.3 Johannes Schlter PHP Roadmap Johannes Schlter 2 PHP 4 - - PowerPoint PPT Presentation

php 5 3
SMART_READER_LITE
LIVE PREVIEW

PHP 5.3 Johannes Schlter PHP Roadmap Johannes Schlter 2 PHP 4 - - PowerPoint PPT Presentation

PHP 5.3 Johannes Schlter PHP Roadmap Johannes Schlter 2 PHP 4 Photo: Patricia Hecht Johannes Schlter 3 PHP 5.2 Photo: Gabriele Kantel Johannes Schlter 4 PHP 5.3 Photo: Jamie Sanford Johannes Schlter 5 PHP 6 Photo: G4Glenno


slide-1
SLIDE 1

PHP 5.3

Johannes Schlüter

slide-2
SLIDE 2

Johannes Schlüter 2

PHP Roadmap

slide-3
SLIDE 3

Johannes Schlüter 3

PHP 4

Photo: Patricia Hecht

slide-4
SLIDE 4

Johannes Schlüter 4

PHP 5.2

Photo: Gabriele Kantel

slide-5
SLIDE 5

Johannes Schlüter 5

PHP 5.3

Photo: Jamie Sanford

slide-6
SLIDE 6

Johannes Schlüter 6

PHP 6

Photo: G4Glenno (flickr)

slide-7
SLIDE 7

Johannes Schlüter 7

New language (syntax) features

❙Namespaces ❙Closures ❙Compile time constants ❙late static binding ❙New operators

❙?: ❙goto ❙NOWDOC syntax, HEREDOC with quotes

❙dynamic static calls

slide-8
SLIDE 8

Johannes Schlüter 8

New functionality

❙New extensions

❙SQLite 3, fileinfo, intl, enchant, phar

❙Added “Unix” functions to Windows

❙fnmatch, symlink, readlink, time_sleep_until, stream_socket_pair, …

❙Improved SPL functionality ❙Improved php.ini handling ❙E_DEPRECATED error level ❙...

slide-9
SLIDE 9

Johannes Schlüter 9

Infrastructure improvements

❙Improved php.ini handling ❙FastCGI always available with CGI SAPI ❙New support for litespeed http server ❙mysqlnd as PHP-specific replacement for libmysql ❙Improved performance

slide-10
SLIDE 10

Johannes Schlüter 10

Namespaces

slide-11
SLIDE 11

Johannes Schlüter 11

Namespaces – The Reasons

❙Class names have to be unique per running script ❙PHP runtime developers tend to add class with generic names

❙“Date”

➔ Class names tend to be long

❙MyFramework_Category_Helper_FooBar

slide-12
SLIDE 12

Johannes Schlüter 12

Namespaces – Design Ideas

❙PHP's namespace implementation is resolving names (mostly) at compile-time

❙They should have no (measurable) impact on the runtime performance ❙new $string; won't know anything about namespaces

❙neither do callbacks or anything else which takes class names as string

❙When using namespaces the namespace declaration has to be in at the beginning of the file ❙There can be multiple namespaces per file

slide-13
SLIDE 13

Johannes Schlüter 13

Namespace-able elements

❙Namespaces can contain classes, functions and constants <?php echo Foo\ANSWER; new Foo\C(); Foo\f(); ?> <?php namespace Foo; const ANSWER = 42; class C { /* ... */ } function f() { } ?>

slide-14
SLIDE 14

Johannes Schlüter 14

Namespace syntax

❙You can use curly braces to define multiple namespaces:

❙<?php namespace Foo { class C { /* ... */ } } namespace Bar { class C { /* ... */ } } ?>

slide-15
SLIDE 15

Johannes Schlüter 15

Namespaces – an example

foo.php <?php namepace MyFramework\someModule; class Foo { /* ... */ } ?>

The compiler translates this to MyFramework\someModule\Foo We can use the full name from within another file

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

slide-16
SLIDE 16

Johannes Schlüter 16

Namespaces – an example

foo.php <?php namepace MyFramework\someModule; class Foo { /* ... */ } ?>

This will be prefixed with the namespace As will this, so we are referring to our previously declared class

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

slide-17
SLIDE 17

Johannes Schlüter 17

Accessing the same Namespace

❙For usage in strings use the magic __NAMESPACE__ constant.

❙call_user_func( array(__NAMESPACE__.'\some_class', 'method'), $param1, $param2, $param3);

❙For accessing elements of the same namespace you may use “namespace”

❙return new namespace\some_class();

slide-18
SLIDE 18

Johannes Schlüter 18

Namespaces and globals

<?php namepace MyFramework\someModule; function strlen($param) { return 0; } echo strlen(“hello world”); echo \strlen(“hello world”); echo some\other\space\strlen(“hello world”); ?>

slide-19
SLIDE 19

Johannes Schlüter 19

Namespaces – Using classes

❙Often you want to use classes from other namespaces

❙Typing the fully qualified name is long

❙“use” creates an alias which can be used in the given file ❙No use foo\*;

slide-20
SLIDE 20

Johannes Schlüter 20

Use example

  • use Foo\Bar;
  • use Foo\Bar as Baz;
  • use RecursiveIteratorIterator as RII;

Use class Bar from Foo and make Bar the alias As above but make Baz the alias It's just an alias so we can create an alias to global classes, too

slide-21
SLIDE 21

Johannes Schlüter 21

Use namespace

❙namespace foo\bar; class class1 {} // foo\bar\class1 class class2 {} // foo\bar\class2 ❙use foo\bar; new bar\class1(); new bar\class2(); ❙No “as” allowed!

slide-22
SLIDE 22

Johannes Schlüter 22

Closures

slide-23
SLIDE 23

Johannes Schlüter 23

Callbacks

❙$data = array( array('sort' => 2, 'foo' => 'some value'), array('sort' => 1, 'foo' => 'other value'), array('sort' => 3, 'foo' => 'one more'), /* … */ ); ❙Task: Sort the array $data using the sort field of the array

slide-24
SLIDE 24

Johannes Schlüter 24

Callbacks

➔Problem: Where to put the callback function?

bool usort (array &$array, callback $cmp_function) This function will sort an array by its values using a user-supplied comparison function.

slide-25
SLIDE 25

Johannes Schlüter 25

eval is evil, so is create_function

❙create_function($a, $b) equals to eval(“function lambda($a) { $b }”);

➔ It is probably insecure, won't work nicely with OpCode

caches, editing the code as string leads to mistakes (no proper highlighting in an editor), ...

slide-26
SLIDE 26

Johannes Schlüter 26

Anonymous functions

$callback = function($a, $b) { if ($a['sort'] == $b['sort']) { return 0; } return ($a['sort'] < $b['sort']) ? -1 : 1; }; usort($data, $callback);

slide-27
SLIDE 27

Johannes Schlüter 27

A closer look

❙var_dump($callback);

  • bject(Closure)#1 (0) {

}

➔ Anonymous Functions/Closures are implemented as

Objects of the type “Closure”

➔ Any object with an __invoke() method can be used as

closure

slide-28
SLIDE 28

Johannes Schlüter 28

Closures

function fancy_count($arr) { $count = 0; $callback = function($dat) use (&$count) { $count++; }; array_walk($arr, $callback); return $count; } echo fancy_count(array(0,1,2,3,4)); // 5

slide-29
SLIDE 29

And More!

slide-30
SLIDE 30

Johannes Schlüter 30

?: Operator

❙Discussed for a long time under the name “issetor” ❙shortcut for $foo ? $foo : $bar; ❙Main usage setting default values for request parameters

❙$id = $_GET['id'] ?: 0;

❙Problem: Might emit an E_STRICT error unlike the “traditional” way

❙$id = isset($_GET['id']) ? $_GET['id'] : 0;

slide-31
SLIDE 31

Johannes Schlüter 31

Dynamic Static Calls

❙As of PHP 5.3 the class name for calling a static class element can be a variable

❙$name = 'Classname'; $name::method();

❙If an an object is passed it's class is used

❙$o = new Class(); $o::method();

❙Use the fully qualified class name in Strings

❙namespace Foo; $name = __NAMESPACE__.'\'.$name; $name::method();

slide-32
SLIDE 32

Johannes Schlüter 32

New error level: E_DEPRECATED

❙Used by PHP to mark functionality which might (or will) be removed with later releases ❙E_STRICT (should) only include errors related to “bad” coding practices ❙E_DEPRECATED is part of E_ALL

❙Continue to develop using error_reporting = E_ALL! ❙Fix errors to ease migration to future releases

slide-33
SLIDE 33

Johannes Schlüter 33

Improved date support

❙Date arithmetics

❙DateInterval represents the difference between to Dates ❙DateTime::add(), DateTime::sub() can be used to apply an interval to a date ❙Dateperiod represents a period of time and allows iteration

slide-34
SLIDE 34

Johannes Schlüter 34

Dateperiod

$begin = new DateTime( '2007-12-31' ); $end = new DateTime( '2009-12-31 23:59:59' ); $interval = DateInterval::createFromDateString( 'last thursday of next month'); $period = new DatePeriod($begin, $interval, $end, DatePeriod::EXCLUDE_START_DATE); foreach ( $period as $dt ) { echo $dt->format( "l Y-m-d H:i:s\n" ); }

slide-35
SLIDE 35

Johannes Schlüter 35

Improved SPL support

❙SPL is the “Standard PHP Library” ❙Until 5.3 it mainly focused on iterators ❙PHP 5.3 introduces

❙SplDoublyLinkedList ❙SplStack ❙SplQueue, SplPriorityQueue ❙SplHeap, SplMinHeap, SplMaxHeap ❙SplFixedArray

slide-36
SLIDE 36

Johannes Schlüter 36

PHAR – PHP Archive

❙Similar to Java's JAR ❙Possibly the future default way for distributing applications ❙PHAR files can use a custom file format or be based on tar or zip archives ❙PHAR includes a flexible front controller system to do the mapping from request to a file inside the phar

slide-37
SLIDE 37

Johannes Schlüter 37

Creating phar archives

try { $phar = new Phar('myapp.phar'); $phar['index.php'] = '<?php echo "Welcome to the index!"; ?>'; $phar['page2.php'] = '<?php echo "This is page 2."; ?>'; } catch (Exception $e) { echo $e; }

slide-38
SLIDE 38

Johannes Schlüter 38

… or from command line

❙$ phar pack -f myapp.phar index.php directory/ ❙$ phar list -f myapp.phar |-phar:///index.php |-phar:///directory/file.php |-phar:///directory/image.png

slide-39
SLIDE 39

Johannes Schlüter 39

Stubs

❙A “stub” is put on top of the file and executed when called ❙$phar->setStub( '<?php echo “Hello World!”; __HALT_COMPILER(); ?>' ); ❙http://example.com/myapp.phar

slide-40
SLIDE 40

Johannes Schlüter 40

webPhar

❙Phar has a front-controller for 1:1 mapping from URLs to files in the phar ❙$phar->setStub( '<?php Phar::webPhar(); __HALT_COMPILER(); ?>' ); ❙http://example.com/myapp.phar/index.php ❙http://example.com/myapp.phar/page2.php ❙http://example.com/myapp.phar/directory/image.jpg

slide-41
SLIDE 41

Johannes Schlüter 41

Garbage Collection

slide-42
SLIDE 42

Johannes Schlüter 42

Reference counting

❙PHP's memory handling is based on reference counting.

❙A PHP variable consists of a label ($label) and the variable container (zval structure) ❙PHP is counting the number of labels pointing to the same variable container

❙<?php $a = new stdClass(); $b = $a; unset($a); unset($b);

reference count = 1 reference count = 2 reference count = 1 reference count = 0

slide-43
SLIDE 43

Johannes Schlüter 43

Cyclic references

❙ $a = new stdClass(); $b = new stdClass(); $a->b = $b; $b->a = $a; unset($a); unset($b); ❙Using reference counting PHP can't see that the

  • bjects aren't referenced from somewhere else

❙Using refcount PHP can't free the memory till the end of the script run

slide-44
SLIDE 44

Johannes Schlüter 44

New garbage collector

❙Now PHP can search for cyclic references from time to time ❙To en-/disable GC use

❙zend.enable_gc php.ini setting ❙gc_enable(), gc_disable()

❙If enabled the GC is trigger automatically or by

❙gc_collect_cycles()

❙For complex applications this can reduce memory usage by the cost of CPU time

❙Unit-Tests!

slide-45
SLIDE 45

Backwards Compatibility

slide-46
SLIDE 46

Johannes Schlüter 46

We try to give our best

❙Sometimes we have to break things ❙Sometimes we make mistakes ❙We need real life tests!

Photo: fireflythegreat (flickr)

slide-47
SLIDE 47

Johannes Schlüter 47

Things known to break

❙New Keywords

❙namespace, closure

❙New global identifiers

❙New extensions ❙New Closure class

❙Extensions dropped

❙ncurses, fpdf, dbase, fbsql, ming, msql

❙Dropped zend.ze1_comptibility_mode

slide-48
SLIDE 48

Johannes Schlüter 48

Function parameters

❙Changes to parameter parsing by internal functions

❙Functions interpreted Objects as Arrays

❙current, next, key, each, reset, end, natsort, natcasesort, usort, uasort, uksort, array_flip, array_unique

❙call_user_func_array stricter about references

❙function foo(&$param) {} ❙call_user_func_array(“foo”, array(23)); ❙call_user_func_array(“foo”, array($variable));

slide-49
SLIDE 49

Summary

slide-50
SLIDE 50

Johannes Schlüter 50

Much new stuff but still faster!?

❙Yes! ❙New scanner (based on re2c instead of flex) ❙Improved internal stack usage ❙Improved access to internal data structures ❙VisualStudio 9 builds for Windows ❙...

slide-51
SLIDE 51

Johannes Schlüter 51

Diagram: Sebastian Bergmann

4.3.11 4.4.7 5.0.5 5.1.6 5.2.5 5.3 time

slide-52
SLIDE 52

Johannes Schlüter 52

Diagram: Sebastian Bergmann

5.1.6 5.2.5 5.3 time

slide-53
SLIDE 53

Johannes Schlüter 53

Links

Downloads: http://downloads.php.net/johannes/ (Source) http://windows.php.net/ (Windows Binaries) http://snaps.php.net/ (Latest snapshots) Documentation: php-src/UPGRADING http://php.net/manual/ http://wiki.php.net/doc/scratchpad/upgrade/53

slide-54
SLIDE 54

????

slide-55
SLIDE 55

Merci pour votre attention!

Johannes Schlüter johannes@php.net

Elephant logo by Vincent Pontier

slide-56
SLIDE 56

intl

slide-57
SLIDE 57

Johannes Schlüter 57

What is intl?

❙Internationalization parts of ICU ❙ICU is the base library for PHP 6's Unicode implementation ❙Collations ❙Output Formatting ❙Normalization

slide-58
SLIDE 58

Johannes Schlüter 58

MessageFormatter

❙$fmt = new MessageFormatter("en_US", "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree"); echo $fmt->format(array(4560, 123, 4560/123)); ❙4,560 monkeys on 123 trees make 37.073 monkeys per tree

slide-59
SLIDE 59

Johannes Schlüter 59

MessageFormatter

❙$fmt = new MessageFormatter("de", "{0,number,integer} Affen auf {1,number,integer} Bäumen ergeben {2,number} Affen pro Baum"); echo $fmt->format(array(4560, 123, 4560/123)); ❙4.560 Affen auf 123 Bäumen ergeben 37,073 Affen pro Baum

slide-60
SLIDE 60

Johannes Schlüter 60

NOWDOC

❙Similar to HEREDOC but does no expanding of variables ❙<?php echo <<<'EOT' This item costs $US 23.42 EOT; ?> ❙This item costs $US 23.42

slide-61
SLIDE 61

Johannes Schlüter 61

HEREDOC with quotes

❙For being consistent with NOWDOCs HEREDOCs allow passing the name escaped by quotes ❙<?php echo <<<”EOT” This item costs $US 23.42 EOT; ?> ❙Notice: Undefined variable: US in ... This item costs 23.42

slide-62
SLIDE 62

Johannes Schlüter 62

Stream-Wrapper Support

❙The include_path ini-Setting can include paths provided by stream wrappers

❙include_path = http://example.com/files:/local:.

❙Mainly thought for phar streams

slide-63
SLIDE 63

Johannes Schlüter 63

mysqlnd

❙PHP-specific replacement for the MySQL Client library (libmysql) ❙Developed by Sun/MySQL ❙Deeply bound to PHP

❙Using PHP memory management ❙Using PHP Streams ❙No external dependencies

❙Not yet another MySQL extension but an internal library sitting below other extnesion

❙Powers ext/mysql, mysqli and pdo_mysql

slide-64
SLIDE 64

Johannes Schlüter 64

mysqlnd

❙To compile PHP using mysqlnd use

❙--with-mysql=mysqlnd ❙--with-mysqli=mysqlnd ❙--with-pdo-mysql=mysqlnd

❙Windows builds use mysqlnd by default

slide-65
SLIDE 65

Johannes Schlüter 65

goto

❙Yes, exactly what the name says ❙But there are few Limitations:

❙You can only jump within the same code unit (the same function, global space from the same file, technically precise: the same OpArray) ❙You can jump out of blocks but not into blocks

❙<?php label: echo 1; goto label; ?>

slide-66
SLIDE 66

Johannes Schlüter 66

Late Static Binding

slide-67
SLIDE 67

Johannes Schlüter 67

Active Record Pattern

“Active record is an approach to accessing data in a database. A database table or view is wrapped into a class, thus an

  • bject instance is tied to a single

row in the table.”

Wikipedia

http://en.wikipedia.org/wiki/Active_record_pattern

slide-68
SLIDE 68

Johannes Schlüter 68

Active Record

❙<?php $user = User::findByID(23); echo “Hello “.$user->getName().”!\n”; $user->setPassword(“verysecret”); $user->save(); ?>

slide-69
SLIDE 69

Johannes Schlüter 69

Late Static Binding

<?php abstract class ActiveRecord { static function findById($id) { $table= /* what's the name to use? */ $query = “SELECT * FROM “.$table .” WHERE id=”.$id; /* .... */ } } class User extends ActiveRecord {} class Entry extends ActiveRecord {} $a = User::findById(2); $b = Entry::findById(4);

$table = get_called_class();

slide-70
SLIDE 70

Johannes Schlüter 70

Late Static Binding II

class Base { public static function m() { self::printName(); static::printName(); } static function printName() { echo __CLASS__; } } } Base::m(); Base Base class Extended extends Base { static function printName() { echo __CLASS__; } } } Extended::m(); Base Extended