Transcript
Page 1: New Features in PHP 5.3

New Features in PHP 5.3

Bradley Holt (http://bradley-holt.com/)

Page 2: New Features in PHP 5.3

Introduction

Page 3: New Features in PHP 5.3

Are you using PHP 5.3?

Page 4: New Features in PHP 5.3

Past, Present & FuturePHP 5.0 brought us a new object model

PHP 5.3 brings us namespaces, closures, late static binding & more

PHP 5.3.99—huh?

Page 5: New Features in PHP 5.3

So What?Speed & memory improvements

Some problems are easier to solve in PHP 5.3

Zend Framework 2.0, Symfony 2.0, Lithium & Doctrine 2.0 will require PHP 5.3

Page 6: New Features in PHP 5.3

New Features

Page 7: New Features in PHP 5.3

Namespaces, Late Static Binding & Closures

Page 8: New Features in PHP 5.3

Namespaces

Page 9: New Features in PHP 5.3

Namespaced Classes

<?phpnamespace btvphp\stuff;class Foo {}$a = new \btvphp\stuff\Foo();$b = new Foo(); // Same as above

Page 10: New Features in PHP 5.3

Namespaced Functions

<?phpnamespace btvphp\stuff;function sayHi(){ return'Hi';}echo \btvphp\stuff\sayHi(); // Hiecho sayHi(); // Hi

Page 11: New Features in PHP 5.3

Namespace AliasingExamples courtesy of Matthew Weier O’Phinney (http://weierophinney.net/matthew/)

Page 12: New Features in PHP 5.3

Aliasing Classes

<?phpnamespace Zend\SignalSlot { class Signals {}}namespace { // global namespace use Zend\SignalSlot\Signals; $signals = new Signals();}

Page 13: New Features in PHP 5.3

Changing the Name

<?phpnamespace Zend\Loader { class PluginBroker {}}namespace { use Zend\Loader\PluginBroker as Broker; $broker = new Broker();}

Page 14: New Features in PHP 5.3

Global Resolution<?phpnamespace Doctrine { class Manager { public static function load() {} }}namespace { \Doctrine\Manager::load();}

Page 15: New Features in PHP 5.3

Late Static Binding

Page 16: New Features in PHP 5.3

The Problem

Page 17: New Features in PHP 5.3

Parent Class<?phpclass Foo { protected static function speak() { return 'Hi'; } public static function sayHi() { return self::speak(); }}

Page 18: New Features in PHP 5.3

Child Class

<?phpclass Bar extends Foo { protected static function speak() { return 'Hello'; }}

Page 19: New Features in PHP 5.3

“Hi” or “Hello”?

<?phpecho Bar::sayHi();

Page 20: New Features in PHP 5.3

“Hi” or “Hello”?

<?phpecho Bar::sayHi(); // Hi

Page 21: New Features in PHP 5.3

The Solution

Page 22: New Features in PHP 5.3

Parent Class<?phpclass Foo { protected static function speak() { return 'Hi'; } public static function sayHi() { return static::speak(); }}

Page 23: New Features in PHP 5.3

Child Class

<?phpclass Bar extends Foo { protected static function speak() { return 'Hello'; }}

Page 24: New Features in PHP 5.3

“Hi” or “Hello”?

<?phpecho Bar::sayHi(); // Hello

Page 25: New Features in PHP 5.3

Closures / Lambda FunctionsSee: http://bit.ly/9LYP3r

Page 26: New Features in PHP 5.3

Variable Assignment

<?php$sayHi = function () { return 'Hi';};echo $sayHi(); // Hi

Page 27: New Features in PHP 5.3

Scope

<?php$sayWhat = 'Hi';$say = function ($toWhom) use ($sayWhat) { return $sayWhat . ', ' . $toWhom;};echo $say('Bradley'); // Hi, Bradley

Page 28: New Features in PHP 5.3

Anonymous Functions<?php$values = array(3, 7, 2);usort($values, function ($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1;});/* [0] => 2 [1] => 3 [2] => 7 */

Page 29: New Features in PHP 5.3

Other Neat Stuff

Page 30: New Features in PHP 5.3

New Bundled ExtensionsPhar (PHP Archive)

Internationalization Functions

Fileinfo: guesses content type & encoding

SQLite version 3

Enchant spelling library

Page 31: New Features in PHP 5.3

Extension Improvements

Page 32: New Features in PHP 5.3

OpenSSLMore OpenSSL functionality available natively within PHP

Faster than do-it-yourself or system calls

Useful if you’re working with OpenID, etc.

Page 33: New Features in PHP 5.3

DateTime ObjectAdd or subtract date intervals

Calculate the difference between two dates

Get or set unix timestamp

See: http://bit.ly/5pDpWI

Page 34: New Features in PHP 5.3

SPL Data StructuresSee: http://bit.ly/bz6pqY

Page 35: New Features in PHP 5.3

SplStackPush & Pop

Last In, First Out (LIFO)

Uses less memory than arrays for big stacks (greater than 5,000 elements)

Page 36: New Features in PHP 5.3

SplQueueEnqueue & Dequeue

First In, First Out (FIFO)

Faster and uses less memory than arrays for most queues

Page 37: New Features in PHP 5.3

SplHeapInsert & Remove

Reorders elements based on comparisons

Faster and uses less memory than arrays for most heaps

Page 38: New Features in PHP 5.3

goto

Courtesy of xkcd: http://xkcd.com/292/

Page 39: New Features in PHP 5.3

New Syntax

Page 40: New Features in PHP 5.3

__invoke()

<?phpclass Foo { public function __invoke($x) { return $x + $x; }}$foo = new Foo();echo $foo(2); // 4

Page 41: New Features in PHP 5.3

__callStatic()<?phpclass Foo { public static function __callStatic($name, $args) { return $name . ' called statically'; }}echo Foo::bar(); // bar called statically

Page 42: New Features in PHP 5.3

__DIR__

<?phpecho dirname(__FILE__);echo __DIR__; // Since PHP 5.3

Page 43: New Features in PHP 5.3

MiscellaneousNowdocs: “Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.”

Improved ternary (?:) operator

Page 44: New Features in PHP 5.3

I’m Sold, What Now?

Page 45: New Features in PHP 5.3

Platform Support

Page 46: New Features in PHP 5.3

LinuxUbuntu 10.10

Fedora 12+

openSuse 11.2+

Red Hat Enterprise Linux (RHEL) 6

Page 47: New Features in PHP 5.3

Mac OS XBundled with Snow Leopard

MacPorts

Homebrew

Page 48: New Features in PHP 5.3

WindowsBinary packages available

WebMatrix Beta 3

Page 49: New Features in PHP 5.3

PHP 5.3 HostingServerGrove

WebMatrix

A2 Hosting

Hostek

Page 50: New Features in PHP 5.3

ResourcesPHP Manual http://php.net/

PHP 5.3.0 Release Announcement http://php.net/releases/5_3_0.php

Migrating from PHP 5.2.x to PHP 5.3.x http://www.php.net/manual/en/migration53.php

Page 51: New Features in PHP 5.3

Questions?

Page 52: New Features in PHP 5.3

Thank YouBradley Holt (http://bradley-holt.com/)

Page 53: New Features in PHP 5.3

New Features in PHP 5.3 by Bradley Holt is licensed under a Creative Commons Attribution 3.0 Unported License.

License


Top Related