Click here to load reader

Introducing PHP Latest Updates

Embed Size (px)

DESCRIPTION

 

Citation preview

  • 1. Introducing PHP Latest Updates Presenter Iftekhar Ahmed Eather Software Engineer Divine IT Limited Source php.net, slideshare.com, wiki, google, git document and others1

2. Topics PHP 5.3 PHP 5.4 PHP 5.5 PSR FlexiGrid2 3. PHP 5.3 Released Date 30-Jun-20093 4. Whats New in PHP 5.3 Late Static BindingClosuresNOWDOCTernary short cutJump LabelOperators, Syntax, Magic Methods, Constants & php.iniGarbage collectorDate and Time4NamespacesOthers 5. Namespaces Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code. Example: 5 6. __ NAMESPACE__ 6 7. Class Resolution namespace MyFrameworksomeModule; class PdoException {} new PdoException(); // MyFrameworkmyModule new PdoException(); // ext/pdo new DateTime(); // class not found! new DateTime(); // works7 8. Aliasing foo/bar.php: some/code.php: 8 9. Late Static Binding Late static bindings can be used to reference the called class in a context of static inheritance. Example: 10. self vs static with LSB class Base { public static function m() { self::printName();class Extended extends Base {static::printName();static function printName()}{static function printName()echo __CLASS__;{}echo __CLASS__; }Extended::m(); // Output: Base Extended} Base::m();10// Output: Base Base 11. Closures 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. Example: 11 12. NOWDOC & HEREDOC Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. Example: NOWDOC echo 'orange', 'bar' => 'apple'];var_dump( array('foo', 'foo' => 'bar') == ['foo', 'foo' => 'bar'] ); //output: true29 30. Function Array De-referencing Another oft-requested syntax addition. Function calls that return an array can now be de-referenced directly: Example: function fruits() { return ['apple', 'banana', 'orange']; } echo fruits()[0]; // Outputs: apple30 31. Instance Method Call Related to function array dereferencing, you can now call a method on object instantiation. And as in previous versions, you can of course still chain method calls, so you can now write code like this: Example: class foo { public $x = 1; public function getX() { return $this->x; } public function setX($val) { $this->x = $val; return $this; } }$X = (new foo)->setX(20)->getX(); echo $X; // 2031 32. Instance Method Call Although, unless your constructor is doing something useful, since the instantiated object is thrown away perhaps you should be using a static method call here instead. If we combine it with the short array syntax and function array de-referencing we can write some really convoluted code: class foo extends ArrayObject { public function __construct($arr) { parent::__construct($arr); } } echo (new foo( [1, [4, 5], 3] ))[1][0];What is output?32 33. Closure Binding Although closure is introduced in 5.3 but binding option include in 5.4 where they interact with objects. Example: class Foo { private $prop; function __construct($prop) { $this->prop = $prop; } public function getPrinter() { return function() { echo ucfirst($this>prop); }; } }33$a = new Foo('bar'); $b = new Foo('pickle'); $func = $a->getPrinter(); $func(); // Outputs: Bar $func = $func->bindTo($b); $func(); // Outputs: Pickle 34. Objects as Functions There is a new magic method called __invoke which can be used like this: Example: class MoneyObject { private $value; function __construct($val) { $this->value = $val; } function __invoke() { return sprintf('$%.2f',$this->value); } } $Money = new MoneyObject(11.02/5*13); echo $Money(); // Outputs: $28.6534 35. Built-in Web Server (CLI) The CLI server is a tiny Web server implementation that you can run from the command line. This web server was designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network. % php -S localhost:8080 /path/to/router.php PHP 5.4.0 Development Server started at Sun Mar 11 13:28:01 2012 Listening on localhost:8080 Document root is /tmp/web Press Ctrl-C to quit.35 36. JsonSerializable Interface You can now control what happens if someone tries to json_encode() your object by implementing the JsonSerializable interface: Example: class Foo implements JsonSerializable { private $data = 'Bar'; public function jsonSerialize() { return array('data'=>$this->data); } } echo json_encode(new Foo); // Outputs: {"data":"Bar"}36 37. Binary Notation To go along with PHPs native support for hexadecimal and octal there is now also binary notation: $mask = 0b010101;37 38. Improved Error Messages Error messages are slightly improved.Before: % php -r 'class abc foo' Parse error: syntax error, unexpected T_STRING, expecting '{' in Command line code on line 1After: % php -r 'class abc foo' Parse error: syntax error, unexpected 'foo' (T_STRING), expecting '{' in Command line code on line38 39. Array to String Conversion Notice If you have ever used PHP you have probably ended up with the word Array randomly appearing in your page because you tried to output an array directly. Whenever an array is directly converted to a string, chances are it is a bug and there is now a notice for that: Example: $a = [1,2,3]; echo $a; //Output notice: Note: Array to string conversion in example.php onlLine 239 40. Short open tag