Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's new in PHP 7

Kapil Sharma
September 05, 2015

What's new in PHP 7

Slides of PHP Reboot meetup on 'What's new in PHP 7', by Kapil Sharm, organized on 5th September 2015 at Ansh Systems Pvt. Ltd.

Kapil Sharma

September 05, 2015
Tweet

More Decks by Kapil Sharma

Other Decks in Technology

Transcript

  1. Speaker • Kapil Sharma • Technical Architect
 Ansh Systems Pvt.

    Ltd. • 11 years experience in web development, mainly PHP. • Twitter: @kapilsharmainfo • Blog: blog.kapilsharma.info • @: Kapil at Kapil Sharma dot info. 2
  2. Topics PHP 7 is (supposed to be) releasing in October

    2015 Where is PHP 6? What is Hack/HHVM/Zephir? What is/was PHP NG? What is new in it? New features in PHP 5.3-5.6. New features in PHP 7. How will it impact me? 3
  3. History of PHP In 1994, Rasmus Lerdorf wrote some CGI

    binaries in 'C' for his 'Personal Home Page'. So PHP, by that time was shortform on 'Personal Home Page'. In 1997, Zeev And Andi rewrite the parser and call it PHP 3. Also PHP become 'PHP: Hypertext Preprocessor'. In 2000, PHP 4 was released, Powered by Zend Engine 1. On 13th July 2004, PHP 5 was released, powered by Zend Engine 2. 9
  4. PHP 6 PHP core developers start discussing PHP 6 since

    2005. From 2008, few books published on PHP 6. By end 2008, PHP 6 was not released, everyone started asking when it will be released. Major issue was unicode support, which was causing delay and problems. 10
  5. –Rasmus Lerdorf “We don't need timelines right now. What we

    need is some hacking time and to bring some fun back into PHP development. It hasn't been fun for quite a while. Once we have a body of new interesting stuff, we can start pondering releases...” 11
  6. PHP 6 On 30th June, 2009, some of PHP 6

    features were back ported & PHP 5.3 was released. Major among these features was namespace. PHP 6 was officially abandoned in 2010. Few other PHP 6 features were later back ported in PHP 5.4. PHP 5.4 - 1st March 2012 PHP 5.5 - 20th June 2013 PHP 5.6 - 28th August 2014 PHP 7 - October 2015 (Supposed) 12
  7. Why PHP 7? Why not PHP 6? 13 Few PHP

    6 books published in 2008-09. Many blog/tutorials about PHP 6, still searchable. If new version would be named as PHP 6, it might have created confusion. So there was a poll on 'What should be next PHP version?' https://wiki.php.net/rfc/php6 Options: PHP 6 or PHP 7 Poll result: PHP 7 won 58:24.
  8. Topics PHP 7 is (supposed to be) releasing in October

    2015 Where is PHP 6? What is Hack/HHVM/Zephir? What is/was PHP NG? What is new in it? New features in PHP 5.3-5.6. New features in PHP 7. How will it impact me? 14
  9. 15

  10. Facebook Facebook was initially developed in PHP. (Still many FB

    pages are in php) Facebook was (is) used by millions of users around the globe. For sites with such user base, execution time is crucial. Being scripting language, PHP (Zend Engine) was slow for them. In 2008, FB make HipHop for PHP Goal: Speed up PHP execution. 16
  11. HHVM There were some issue with HipHop for PHP In

    2010, they decided to make HHVM - HipHop Virtual Machine In 2012, Facebook claimed HHVM have 3x-10x speed boost and 1/2 memory footprint as compare to PHP+ACP. 17
  12. Hack Soon facebook came with Hack, a new programming language.

    Every valid PHP program is valid Hack program. Also contains many features that PHP missed. Return type Type hinting for primitive data type. Concise code. Collection support And many more (Example: Vector and Map) 18
  13. Zephir Zephir is an open source, high-level/domain specific language designed

    to ease the creation and maintainability of extensions for PHP with a focus on type and memory safety. 20
  14. Challenges In short, there were lot of challenges in front

    of PHP Increase execution speed. Decrease memory consumption. New features. 21
  15. PHP-NG PHP New Generation. Internal branch in php repo with

    goal of improved performance and efficient memory usage. PHP NG is not the JIT but internal refactoring to make JIT possible in future. PHPNG never released but later on, it is merged to master and becomes base of PHP 7. 22
  16. Topics PHP 7 is (supposed to be) releasing in October

    2015 Where is PHP 6? What is Hack/HHVM/Zephir? What is/was PHP NG? What is new in it? New features in PHP 5.3-5.6. New features in PHP 7. How will it impact me? 24
  17. What was new in PHP 5.3? Most important: * Namespace

    Late static binding Jump labels (limited goto) * Closures (Lambda/Anonymous function) Now doc Constants outside class using const keyword Shorthand ternary operator New magic method: __callStatic & __invoke 25
  18. Namespace Have you worked with class names like - Zend_Validate_Db_NoRecordExists

    - Zend_Auth_Storage_Session - Zend_Acl_Role_Registry Why such a long names? 26
  19. Zend_Auth_Storage_Session Session, a very common class name. We can have

    custom class called 'Session'. $session = new Session(); Is it our custom class or Session class form ZF1? Well we can't say. $session = new Zend_Auth_Storage_Session() We can now easily say which class we are referring. 27
  20. Auto loading ZF 1 comes with autoloading support which allow

    us using $session = new Zend_Auth_Storage_Session(); Internally auto loader will split class with '_' and use it as path. Still 'Zend_Auth_Storage_Session' is ugly class name, isn’t it? 29
  21. Namespace - Name space in itself are easy. - However

    it drastically changed the way applications are structured. - This force big project to stop backward compatibility. - Example Symfony 2 and Zend Framework 2. 33
  22. –Taken from PHP manual 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. Closures or Lambda or Anonymous functions 34
  23. Example My favourite fruit is Mango My favourite color is

    Green My favourite day is Obviously Sunday 35 Little aside, can we do same example without foreach? array_map array_walk (for associative arrays)
  24. Example with array_walk Advantage: It is faster than foreach (Conditionally)

    Disadvantage, I've one more function in scope. 36
  25. Lambda We needed function only once, make it immediately and

    it is not available anymore after operation. Few more new things in PHP 5.3 but namespace was most important. Lets move to PHP 5.4 37
  26. What was new in PHP 5.4? * Traits (most important)

    Build-in web server (Allows quick local testing) Short array syntax Function array dereferencing Closures can use $this <?= available regardless of short_open_tag setting. 38
  27. Traits Minimising code duplication is one of major goal of

    OOP. PHP, being single inheritance model, have some limitation. If you similar code in two unrelated classes (Cant extend same class) Refactoring: Extract class. but it do not fit every where. PHP 5.4 had new solutions; Traits
  28. Traits Traits is a mechanism for code reuse in single

    inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. 40
  29. What was new in PHP 5.5 * New Password hashing

    API Generators finally keyword List support in foreach Array and string literal dereferencing Class name resolution via ::class Non scalar keys support for foreach. 43
  30. Password Hashing How do you hash your password? $hash =

    md5($password); Not safe There are lot of md5 database. $hash = md5($password . $salt); Still not safe, not because cryptographic weaknesses but because it is too fast Too fast for Brute-force attack. 44
  31. Password hashing PHP 5.5 came with simple password hashing api.

    To hash password Verifying password is also easy: There is much more in password hashing, look at manuals for details. 45
  32. Whats new in PHP 5.6 Constant expression Constant array ...

    operator (for variable length parameters) use const and use function __debugInfo() magic method 46
  33. PHP 5.7 Well not released and will not be released.

    Proposed PHP 5.7 was: Prepare our users to 7 No or very little additions, development must be focused on 7 Keep to our release process Supposed to add the necessary notices or deprecation notices to prepare our users to move to 7. However in voting, it was voted down. So no PHP 5.7. 47
  34. PHP 7 (For Stake holders) Internal goals 100%+ performance gain

    on most real world applications. Lower memory usages. 48
  35. PHP 7 (For developers) Type hinting for primitive data types

    Return type Spaceship operator Anonymous Class Null coalesce operator (??) Exceptions on Fatals Many new reserved keywords 49
  36. Typehint Sum of 2 and 3 is 5 Sum of

    2 and kapil is 2 Internal type casting 50
  37. Anonymous classes 1. Quickly implement an interface for a dependency

    2. Simple mocks without a mocking library 56
  38. Null coalescing operator (??) New ‘Null coalescing operator’. $result =

    $a ?? ‘none'; If $a is defined, $result = $a otherwise $result = ‘none’. Warnings: $result = null ?? ‘none’; means ‘none’. $result false ?? ‘none'; means false. 57
  39. Exception on fatal errors Handling fatal errors in php was

    next to impossible. In PHP 7, exception will be thrown on both fatal (E_ERROR) and recoverable errors (E_RECOVERABLE_ERROR) 58
  40. New reserved keywords int float bool string true false null

    resource object scalar mixed numeric 59
  41. Setup PHP 7 dev env mkdir $HOME/php7 cd $HOME/php7 git

    clone https://git.php.net/repository/php-src.git cd php-src ./buildconf ./configure --prefix=$HOME/php7/usr —with-config-file-path= $HOME/php7/usr/etc --enable-mbstring --enable-zip --enable- bcmath --enable-pcntl --enable-ftp --enable-exif -- enable-calendar --enable-sysvmsg --enable-sysvsem -- enable-sysvshm --enable-wddx --with-curl --with-mcrypt -- with-iconv --with-gmp --with-pspell --with-gd --with-jpeg- dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm- dir=/usr --with-freetype-dir=/usr --with-t1lib=/usr --enable- gd-native-ttf --enable-gd-jis-conv --with-openssl --with- mysql=/usr --with-pdo-mysql=/usr --with-gettext=/usr --with- zlib=/usr --with-bz2=/usr --with-recode=/usr --with-mysqli=/ usr/bin/mysql_config 60
  42. Setup PHP 7 dev env Obviously do not set PHP

    7 on local development box. Use vagrant. I personally used Homestead for that (As can be seen in examples here) Use cloud server. I successfully compiled PHP 7 on ‘koding’ free account. However compilation could not be done on Cloud 9 free account. 61