Introduction to PHP 5.4
Getting ready for it

Giovanni Derks - http://derks.me.uk
A little bit of history
● 30/06/2009: PHP 5.3
● 01/03/2012: PHP 5.4
● 20/06/2013: PHP 5.5
Previously, on PHP ...
To avoid confusion it is
useful to list the major
changes of PHP 5.3:

●
●
●
●
●
●
●
●
●

Support f...
More information on 5.3
1.
2.
3.
4.

http://php.net/manual/en/migration53.new-features.php
http://www.sitepoint.com/whats-...
What have been removed
from PHP 5.4
●
●
●
●
●

safe mode;
register_globals;
magic quotes;
get_magic_quotes_gpc() will alwa...
What’s new in PHP 5.4
Main new features:
●
●
●
●
●
●
●
●

multibyte support is enabled by default and default_charset has ...
Before we dig into it
How to switch to php 5.4 on Gentoo:
# eselect php list apache2

How to use the built in server:
●

#...
Improved performance
Short echo tags
Code:

Result:

<?="Hello world";?>

Hello world
Short array syntax
Code:

Result:

<?php $a = [1, 2, 3, 4]; ?>

array(2) {
["one"]=>int(1)

or

["two"]=>int(2)
<?php $a =...
Function array dereferencing
Code:

Result:

<?php

b

function fncReturnArray() {
return ["a", "b", "c", "d"];
}

echo fn...
'REQUEST_TIME_FLOAT'
The timestamp of the start of the request, with microsecond precision.

Code:
<?php
echo "Page loaded...
Upload progress
PHP 5.4 provides the ability to track the upload progress of individual files.
The upload progress will be...
Class::{expr}() Syntax
Code:

Result:

class test01 {

OMG! ...36 is even

public static function printIsEven($num) {
echo...
$this in closures + class member access on
instantiation
Code:

Result:

class test02 {

hello world

private $_test = "he...
Traits: code
// (from: http://en.wikipedia.org/wiki/Trait_
(computer_programming))

// the template:

// -- >

// new func...
Traits: result
hello world

More information:
● http://en.wikipedia.org/wiki/Trait_(computer_programming)
● http://www.sit...
Getting ready
If you’re still using PHP 5.3, here are a few things you can do to prepare for the
switch:
●
●
●
●
●

use sh...
Useful links
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

http://php.net/manual/en/migration54.new-features.php
http://...
Thank you!

Get in touch:
●
●
●

http://derks.me.uk
https://twitter.com/giovdk21
http://uk.linkedin.com/in/giovanniderks/
Upcoming SlideShare
Loading in …5
×

An introduction to PHP 5.4

2,310 views

Published on

A brief introduction to PHP 5.4 and a few suggestions on how getting ready to it.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,310
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

An introduction to PHP 5.4

  1. 1. Introduction to PHP 5.4 Getting ready for it Giovanni Derks - http://derks.me.uk
  2. 2. A little bit of history ● 30/06/2009: PHP 5.3 ● 01/03/2012: PHP 5.4 ● 20/06/2013: PHP 5.5
  3. 3. Previously, on PHP ... To avoid confusion it is useful to list the major changes of PHP 5.3: ● ● ● ● ● ● ● ● ● Support for namespaces; Support for Late Static Bindings (static::); Support for jump labels (limited goto); Support for native Closures (Lambda/Anonymous functions); Constants can now be declared outside a class using the const keyword; Dynamic access to static methods ($a = "C"; echo $a::$foo;); Nested exceptions; Nowdocs support (<<<'EOD' … EOD;); New magic methods, __callStatic() and __invoke().
  4. 4. More information on 5.3 1. 2. 3. 4. http://php.net/manual/en/migration53.new-features.php http://www.sitepoint.com/whats-new-php-5-3/ http://www.ibm.com/developerworks/opensource/library/os-php-5.3new1/index.html http://phptrycatch.blogspot.co.uk/2012/07/throw-your-own-nested-exceptions.html
  5. 5. What have been removed from PHP 5.4 ● ● ● ● ● safe mode; register_globals; magic quotes; get_magic_quotes_gpc() will always return false; the ini option allow_call_time_pass_reference. (A more comprehensive list can be found in the documentation linked at the end of this presentation)
  6. 6. What’s new in PHP 5.4 Main new features: ● ● ● ● ● ● ● ● multibyte support is enabled by default and default_charset has been changed from ISO-8859-1 to UTF-8; improved performance; echo short tags are always available (regardless the short_open_tag ini setting); closures now support $this; class member access on instantiation; upload progress available in the $_SESSION superglobal; Class::{expr}() syntax; built-in development web server.
  7. 7. Before we dig into it How to switch to php 5.4 on Gentoo: # eselect php list apache2 How to use the built in server: ● # eselect php set apache2 X (X = item number in the list of available php versions) You have to run `/etc/init.d/apache2 restart' for the changes to take effect ● ● switch also the command line version of php to 5.4: # eselect php list cli [...] change directory to your project root folder run php -S localhost:8000 or make it listen on any IP (php -S 0.0.0.0:8000) # /etc/init.d/apache2 restart # eselect php list apache2 http://wiki.gentoo.org/wiki/PHP http://php.net/manual/en/features.commandline. webserver.php
  8. 8. Improved performance
  9. 9. Short echo tags Code: Result: <?="Hello world";?> Hello world
  10. 10. Short array syntax Code: Result: <?php $a = [1, 2, 3, 4]; ?> array(2) { ["one"]=>int(1) or ["two"]=>int(2) <?php $a = ['one' => 1, 'two' => 2]; ?> }
  11. 11. Function array dereferencing Code: Result: <?php b function fncReturnArray() { return ["a", "b", "c", "d"]; } echo fncReturnArray()[1]; ?>
  12. 12. 'REQUEST_TIME_FLOAT' The timestamp of the start of the request, with microsecond precision. Code: <?php echo "Page loaded in:" . (microtime(true) $_SERVER['REQUEST_TIME_FLOAT']); ?> Result: Page loaded in: 0.0065569877624512
  13. 13. Upload progress PHP 5.4 provides the ability to track the upload progress of individual files. The upload progress will be available in the $_SESSION superglobal when an upload is in progress. More information: ● ● http://php.net/manual/en/session.upload-progress.php https://github.com/chemicaloliver/PHP-5.4-Upload-Progress-Example
  14. 14. Class::{expr}() Syntax Code: Result: class test01 { OMG! ...36 is even public static function printIsEven($num) { echo "OMG! ...{$num} is even"; } public static function printIsOdd($num) { echo "OMG! ...{$num} is odd"; } } $sec = date('s'); echo test01::{ $sec % 2 === 0 ? 'printIsEven' : 'printIsOdd' }($sec);
  15. 15. $this in closures + class member access on instantiation Code: Result: class test02 { hello world private $_test = "hello world"; function testThisInAnonFunc() { $myVal = call_user_func(function () { return $this->_test; }); echo $myVal; } } (new test02)->testThisInAnonFunc();
  16. 16. Traits: code // (from: http://en.wikipedia.org/wiki/Trait_ (computer_programming)) // the template: // -- > // new function: trait_exists trait TSingleton { if (trait_exists('TSingleton')) { private static $_instance = null; // can also be used in already extended public static function getInstance() { if (null === self::$_instance) // classes; test02 is from prev. example { class test03 extends test02 { self::$_instance = new self(); use TSingleton; } } return self::$_instance; } } test03::getInstance()->testThisInAnonFunc(); // -- > }
  17. 17. Traits: result hello world More information: ● http://en.wikipedia.org/wiki/Trait_(computer_programming) ● http://www.sitepoint.com/using-traits-in-php-5-4/
  18. 18. Getting ready If you’re still using PHP 5.3, here are a few things you can do to prepare for the switch: ● ● ● ● ● use short echo tags; work with safe_mode turned off; use date_default_timezone_set(); do not rely on magic quotes; check that you are not using session_register(), session_unregister() or session_is_registered().
  19. 19. Useful links 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. http://php.net/manual/en/migration54.new-features.php http://www.php.net/manual/en/migration54.incompatible.php http://www.sitepoint.com/whats-new-in-php-54/ http://net.tutsplus.com/tutorials/php/php-5-4-is-here-what-you-must-know/ http://www.king-foo.be/2012/01/php-5-4-whats-new/ http://www.larryullman.com/2012/05/09/whats-new-in-php-5-4/ http://php.webtutor.pl/en/2011/09/27/whats-new-in-php-5-4-a-huge-list-of-major-changes/ http://www.webdigi.co.uk/blog/2012/cool-new-stuff-in-php-5-4/ http://blog.ircmaxell.com/2011/07/are-traits-new-eval.html http://www.slideshare.net/predominant/php-54-begin-your-love-affair-with-traits-10118525 http://w3techs.com/technologies/details/pl-php/5/all http://www.sitepoint.com/using-traits-in-php-5-4/ http://net.tutsplus.com/tutorials/php/functional-programming-in-php/ http://www.php.net/manual/en/ini.core.php#ini.allow-call-time-pass-reference
  20. 20. Thank you! Get in touch: ● ● ● http://derks.me.uk https://twitter.com/giovdk21 http://uk.linkedin.com/in/giovanniderks/

×