php 5.3 overview

38
PHP 5.3 OVERVIEW 6/8/2010 - Dallas PHP Jake Smith

Upload: jsmith92

Post on 02-Dec-2014

1.122 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: PHP 5.3 Overview

PHP 5.3 OVERVIEW6/8/2010 - Dallas PHP

Jake Smith

Page 2: PHP 5.3 Overview

PERFORMANCE

• Over 140 bug fixes

• 40%+ improvement with PHP on Windows

• 5% - 15% overall performance improvement

• MD5 roughly 15% faster

• Constants move to read-only memory

• Drupal 20% faster, Wordpress 15% faster

Page 3: PHP 5.3 Overview

ADDITIONS

• New error_reporting E_DEPRECATED

• Garbage collection

• MySQLnd (Native Driver)

• No longer uses libmysql

• No PDO support (currently)

• MySQL version 4.1+

Page 4: PHP 5.3 Overview

BACKWARDS COMPATIBILITY

• EREG Family is now E_DEPRECATED

• Use the Pearl Compatible (PCRE)

• __toString does not accept arguments/parameters

• Magic methods must be public and can not be static

• __call is now invoked on access to private/protected methods

• Classes can not be named Namespace or ClosureSOURCES: http://us2.php.net/manual/en/migration53.incompatible.php

Page 5: PHP 5.3 Overview

MAGIC METHODS IN 5.3<?php

class Backwards { public function __call($method, $value) { echo "Call Magic Method<br />\n"; } private function __get($method) { } private function __set($method, $value) { } private function getElements() { echo "Get Elements<br />\n"; }}

$bc = new Backwards();

$bc->getElements();

Page 6: PHP 5.3 Overview

CHANGES IN PHP.INI

• INI Variables

• Per Folder/Per Site ini settings

• User specified ini files

Page 7: PHP 5.3 Overview

.INI VARIABLESerror_dev = E_ALLerror_prod = E_NONE

[HOST=dev.mydomain.com]error_reporting = ${error_dev}

[HOST=mydomain.com]error_reporting = ${error_prod}

[PATH=/var/www/vhosts/myotherdomain.com]error_reporting = ${error_prod}

# User Defined ini. Place in web root. Set to blank to disableuser_ini.filename = .user.ini

user_ini.cache_ttl = 300

Page 8: PHP 5.3 Overview

SLOW ADOPTION

• Open Source projects were initially not compatible with PHP 5.3

• Currently most major Open Source software (Wordpress, Drupal, Joomla and Magento) work in PHP 5.3

• Key plugins are lacking behind

Page 9: PHP 5.3 Overview

PECL ADDITIONS

• PECL Added

• FileInfo, Intl, Phar, MySQLnd, SQLite3

• PECL Removed

• ncurses, FPDF, dbase, fbsql, ming

SOURCES: http://php.net/releases/5_3_0.php

Page 10: PHP 5.3 Overview

SPL ADDITIONS

• GlobIterator - Iterator utilizing glob, look up glob function

• SplFixedArray - Fixed size/dimension array

• SplQueue

• SplHeap

• SplStack

SOURCES: http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

Page 11: PHP 5.3 Overview

NEW CONSTANTS

• __DIR__

• No more dirname(__FILE__);

• __NAMESPACE__

• Current namespace

Page 12: PHP 5.3 Overview

NEW OPERATORS

• Ternary Operator ?:

• Goto

• NOWDOC

Page 13: PHP 5.3 Overview

TERNARY OPERATOR

<?php// Before PHP 5.3$action = $_POST['action'] ? $_POST['action'] : 'Default Action';

// Now in PHP 5.3 (less time)$action = $_POST['action'] ?: 'Default Action';

Page 14: PHP 5.3 Overview

GOTO

SOURCES: http://xkcd.com/292/

Page 15: PHP 5.3 Overview

GOTO EXAMPLE

<?phpfor($i=0,$j=50; $i<100; $i++) { while($j--) { if($j==17) goto end; } }

echo "i = $i";

end: // End echo 'j hit 17';

SOURCES: http://php.net/manual/en/control-structures.goto.php

Page 16: PHP 5.3 Overview

NOWDOC VS. HEREDOC

• NOWDOC works just like HEREDOC, except it does not evaluate PHP variables

<?php$myVar = 'testing';// OUTPUT: Here is my text testing$longString = <<<HEREDOCHere is my text $myVarHEREDOC;

// OUTPUT: Here is my text $myVar$longString = <<<'NOWDOC'Here is my text $myVar'NOWDOC';

Page 17: PHP 5.3 Overview

DATE/TIME OBJECT ADDITIONS

• New functions/methods added to Date/Time Object

• date_add, date_sub and date_diff

SOURCES: http://www.php.net/manual/en/class.datetime.php

<?php$date = new DateTime('2000-01-01');$date->add(new DateInterval('P10D'));echo $date->format('Y-m-d') . "\n";

// OR

$date = date_create('200-01-01');date_add($date, date_interval_create_from_date_string('10 days'));echo date_format($date, 'Y-m-d');

Page 18: PHP 5.3 Overview

NEW METHODS

• Functors/__invoke

• Dynamic Static Method

• __callStatic

• get_called_class()

Page 19: PHP 5.3 Overview

__INVOKE

<?phpclass Functor { public function __invoke($param = null) { return 'Hello Param: ' . $param; }}

$func = new Functor();echo $func('PHP 5.3'); // Hello Param: PHP 5.3

Page 20: PHP 5.3 Overview

DYNAMIC STATIC METHOD

<?phpabstract class Model{ const TABLE_NAME = ''; public static function __call($method, $params) { // Run logic return $this->$method($criteria, $order, $limit, $offset); }}

Page 21: PHP 5.3 Overview

DYNAMIC STATIC METHOD

<?phpabstract class Model{ const TABLE_NAME = ''; public static function __callStatic($method, $params) { // Run logic return static::$method($criteria, $order, $limit, $offset); }}

Page 22: PHP 5.3 Overview

__CALLSTATIC

• __callStatic works exactly like __call, except it’s a static method

• Acts as a catch all for all undefined methods (get or set)

<?phpabstract class Model{ const TABLE_NAME = ''; public static function __callStatic($method, $params) { // Run logic return static::$method($criteria, $order, $limit, $offset); }}

Page 23: PHP 5.3 Overview

GET_CLASS_CALLED

• get_called_class returns the class name that called on the parent method

Page 24: PHP 5.3 Overview

GET_CLASS_CALLED<?php namespace App { abstract class Model { public static function __callStatic($method, $params) { // Search Logic $method = $matches[1]; return static::$method($criteria, $order, $limit, $offset); } public static function find($criteria = array(), $order = null, $limit = null, $offset = 0) { $tableName = strtolower(get_class_called()); // get_class_called will return Post } } } namespace App\Models { class Post extends \App\Model {} } // Returns all the posts that contain Dallas PHP in the title $posts = Post::findByTitle('Dallas PHP');?>

Page 25: PHP 5.3 Overview

LATE STATIC BINDING

• This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will no longer be resolved using the class where the method is defined but it will rather be computed using runtime information.

SOURCES: http://us.php.net/lsb

Page 26: PHP 5.3 Overview

LSB BEFORE PHP 5.3<?php

class Model { const TABLE_NAME = ''; public static function getTable() { return self::TABLE_NAME; }}

class Author extends Model { const TABLE_NAME = 'Author';}

class Post extends Model { const TABLE_NAME = 'Post'}

// sadly you get nothingecho Post::TABLE_NAME;

Page 27: PHP 5.3 Overview

LSB PHP 5.3.X

• static keyword to save the day!<?php

class Model { const TABLE_NAME = ''; public static function getTable() { return static::TABLE_NAME; }}

class Author extends Model { const TABLE_NAME = 'Author';}

class Post extends Model { const TABLE_NAME = 'Post'}

// sadly you get nothingecho Post::TABLE_NAME;

Page 28: PHP 5.3 Overview

LAMBDA IN PHP 5.3

• Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

• Good function examples: array_map() and array_walk()

<?php$string = 'Testing';$array = array('hello', 'world', 'trying', 'PHP 5.3');$return = array_walk($array, function($v,$k) { echo ucwords($v);});

Page 29: PHP 5.3 Overview

LAMBDA EXAMPLE (JQUERY)

$('.button').click(function() { $(this).hide();});

Page 30: PHP 5.3 Overview

CLOSURE<?php// Cycle factory: takes a series of arguments// for the closure to cycle over.function getRowColor (array $colors) { $i = 0; $max = count($colors); $colors = array_values($colors); $color = function() use (&$i, $max, $colors) { $color = $colors[$i]; $i = ($i + 1) % $max; return $color; }; return $color;}

$rowColor = getRowColor(array('#FFF', '#F00', '#000'));echo $rowColor() . '<br />';echo $rowColor() . '<br />';echo $rowColor() . '<br />';echo $rowColor() . '<br />';echo $rowColor() . '<br />';echo $rowColor() . '<br />';

Page 31: PHP 5.3 Overview

NAMESPACES

• Before PHP 5.3

• PHP didn’t have namespaces, so we created a standard: “Zend_Auth_Adapter_DbTable”

• PEAR naming convention

• Easier for autoloaders

Page 32: PHP 5.3 Overview

DEFINING A NAMESPACE

<?php namespace MyApp\Util; class String { public function formatPhone($phone) { // Regex phone number return true; } }

Page 33: PHP 5.3 Overview

“USE” A NAMESPACE

<?phpuse MyApp\Util\String as String;

$str = new String(); if ($str->formatPhone('123-456-7890')) { echo 'ITS TRUE'; }

Page 34: PHP 5.3 Overview

DEFINING MULTIPLE NAMESPACES

<?phpuse Framework\Controller as BaseControlleruse Framework\Model as BaseModelnamespace MyApp\Controllers;

class UsersController extends BaseController{ }

namespace MyApp\UserModel;class User extends BaseModel{ }

<?phpuse Framework\Controller as BaseControlleruse Framework\Model as BaseModelnamespace MyApp\Controllers {

class UsersController extends BaseController{ }

}

namespace MyApp\UserModel {class User extends BaseModel{ }

}

One Way Preferred Way!

Page 35: PHP 5.3 Overview

WHAT IS GLOBAL SCOPE?

• Global Scope is your “root level” outside of the namespace

<?phpnamespace Framework;

class DB{ public function getConnection() { try { $dbh = new \PDO('mysql:dbname=testdb;host=localhost', 'root', 'pass'); } catch (\Exception $e) { echo 'Default Exception'; } }}

$db = new DB();$db = $db->getConnection();

Page 36: PHP 5.3 Overview

ORM EXAMPLE<?php namespace App { abstract class Model { const TABLE_NAME = ''; public static function __callStatic($method, $params) { if (!preg_match('/^(find|findFirst|count)By(\w+)$/', $method, $matches)) { throw new \Exception("Call to undefined method {$method}"); } echo preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $matches[2]); $criteriaKeys = explode('_And_', preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $matches[2])); print_r($matches); $criteriaKeys = array_map('strtolower', $criteriaKeys); $criteriaValues = array_slice($params, 0, count($criteriaKeys)); $criteria = array_combine($criteriaKeys, $criteriaValues); $params = array_slice($params, count($criteriaKeys)); if (count($params) > 0) { list($order, $limit, $offset) = $params; }

$method = $matches[1]; return static::$method($criteria, $order, $limit, $offset); } public static function find($criteria = array(), $order = null, $limit = null, $offset = 0) { echo static::TABLE_NAME; echo $order . ' ' . $limit . ' ' . $offset; } } } namespace App\Models { class Posts extends \App\Model { const TABLE_NAME = 'posts'; } }

Page 37: PHP 5.3 Overview

QUESTIONS?

Page 38: PHP 5.3 Overview

THANKS FOR LISTENINGContact Information[t]: @jakefolio[e]: [email protected][w]: http://www.jakefolio.com