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

FiveOh to FiveFive

FiveOh to FiveFive

Did you spend months begging your boss to jump into PHP 5 after being stranded in legacy hell of PHP 4, only to get stranded again somewhere in the middle of the 5.x sea? Lets take a look at PHP 5 from 5.0 to 5.5 to see what major changes you may need to consider before begging to update to the latest and greatest again.

The recorded meetup: http://www.youtube.com/watch?v=jCJI8HcV_EM&hd=1

Bob Majdak Jr

February 11, 2014
Tweet

More Decks by Bob Majdak Jr

Other Decks in Programming

Transcript

  1. Fear. The Mind Killer. There are way too many changes

    to catch them all. We'd have to totally rewrite.
  2. Bugs. The Confidence Killer. Bite me once, shame on you.

    Bite me twice, shame on me. And also you. Meanie.
  3. NCC-1701. The Budget Killer. We can't just boldy go where

    lots of people have gone before, we finally just sold this thing to a client.
  4. “Most improvements in PHP 5.x.x have no impact on existing

    code.” - The PHP Change Log “Most improvements in PHP 5.x.x need to impact your existing code, such that it also improves.” - This Guy
  5. PHP 4 to PHP 5 • For general every day

    code? Not as scary as it sounds. • Depending on a lot of extensions? That is a little rougher. Core extensions migrated at the same time, third party ones lagged.
  6. PHP 5.0 New Features • Full OOP – constructors, destructors,

    access keywords, other magic methods, instanceof operator • Object Pass-By-Reference by default – time to delete a lot of &s • New: MySQLi • Standardised XML tools • SPL Classes
  7. PHP 5.0 New Features • SPL! – ArrayIterator – DirectoryIterator

    – Various other Iterators – Various Exceptions • Autoloading! • Exception Handling!
  8. PHP 5.0 New Features • array_combine • array_udiff • array_walk_recursive

    • stream_socket_accept • stream_socket_client • stream_socket_server • str_split • strbrk • substr_compare • file_put_contents • get_headers • headers_list • imagefilter • php_check_syntax • php_strip_whitespace • scandir • ldap_sasl_bind • curl_copy_handle • ftp_alloc • http_build_query • imap_getacl • pcntl_wait • proc_nice • iconv_strlen • iconv_strpos
  9. PHP 5.0 Got-Ya • Objects without properties no longer evaluate

    as empty(). • Objects explicitly passed by &reference are now references of references and will wreak havoc on the internal optimiser. • get_class() returns the name as defined, not all lowercase as previously. • In PHP 4 it was ok to require() a file multiple times even if it defined functions. In PHP 5 you will get “function already defined” errors – so use require_once.
  10. PHP 5.0 Got-Ya (cont.) • The new instanceof operator is

    actually “broken” if you are using autoloading. If the class you specify cannot be found, it will attempt __autoload() fatal error. Also effects class_exists(). • Lots of functions like the array_ family of functions are stricter, if you try to give them things they do not want, they will toss more error messages out.
  11. PHP 5.0 to PHP 5.1 • More Fast! • Unified

    database access API. (PDO) • instanceof, class_exists – fixed. • Reference handling cleaned up a little more with nested functions. (few less E_NOTICE) • Fully rewritten Date and Time stuff to try and be smarter based on the system being run upon. • SPL: SplFileInfo family of classes.
  12. PHP 5.1 Got-Ya • Functions which expect integers, and you

    give it a broken string like “ 0123” will start throwing extra notices. • If you tried to define “private abstract” methods, don't. They got rid of it, and you'll get error messages. • Protected/Private methods defined in interfaces will also throw more errors. • strtotime – returns boolean FALSE instead of -1 on fail.
  13. PHP 5.1 Got-Ya • Changing a methods signature in an

    extended class will throw an E_STRICT. • All core extensions had their constants rewritten. E.g. PDO_PARAM_BOOL is now PDO::PARAM_BOOL • Lots of fringe extensions have been removed or replaced. – http://www.php.net/manual/en/migration51.extensions.php • Not setting date.timezone in your PHP .INI will throw notices, even though they just redid Date/Time to be smarter and default to UTC.
  14. PHP 5.1 to 5.2 • More fast! • New default

    extensions: filter, json, zip • htmlentities, htmlspecialchars, now can double encode. • __toString() works in ALL string contexts. • setcookie now has httponly argument. • memory_get_usage now has real use argument. • date() - added “u” format for milliseconds. • New SPL classes: DateTime, DateTimeZone, RegexIterator
  15. PHP 5.2 Got-Ya • SplFileObject::getFilename now only returns the filename,

    not the full path now. Combine with getPath(). • 5 % 0 – modulus operations will now throw the division by 0 errors like... division. • E_RECOVERABLE_ERROR was added to E_ALL. • CLI no longer checks current directory for PHP .INI automatically. `php -c .`
  16. PHP 5.2 Got-Ya (cont.) • Even more functions will throw

    E_NOTICE if you provide the wrong data type, like an object when the function wants an array. Clean these up as you see them. • `abstract static` is no longer a thing.
  17. PHP 5.2 to 5.3 • More, more fast! • New

    garbage collector. • New structure: goto! • New structure: namespace! • New Feature: Late static binding! • New Feature: Native Lambda/Closures! • New SPL classes: DateInterval, Phar, FilesystemIterator, SplHeap, SplQueue, SplStack • New default extensions: Phar, Enchant, Fileinfo, INTL, SQLite3
  18. PHP 5.3 - MySQL • New MySQL driver: mysqlnd –

    requires MySQL 4.1+ – implements updated connection security – should require no changes unless you used magic my.cnf loading. Add call to mysqli_options to load one. • mysql_escape_string is deprecated. – update to mysql_real_escape_string
  19. PHP 5.3 – GOTO (cont) • Not your father's GOTO.

    – Must be same file and same context/scope. – No jumping into and out of function definitions. – No jumping into loops. • Still. Use with care. Still too easy to write painful to follow code.
  20. PHP 5.3 - Namespace • We can stop polluting the

    root namespace. • Do not have to worry (nearly as much) about conflicting class names across third party packages.
  21. PHP 5.3 – Namespace (cont.) • Putting it all together:

    – directory structure, namespaces, autoloading. – %APP%/src/People/Bob.php – $person = new People\Bob; • http://www.php.net/manual/en/language.oop5.au toload.php
  22. PHP 5.3 – Late Static Binding • Allows you to

    craft base classes which fully expect you to extend their base components later on, and follow a Factory style or just want to allow extended versions to reference themselves nice. • This was not impossible before, but as of now it is crazy simple. • self:: vs static:: – self:: and __CLASS__ will report the class they are used IN. – static:: will report the class the it is used FROM.
  23. PHP 5.3 - Closures • Create functions on the fly,

    which can be assigned to variables or passed directly to anything expecting a callable argument. • Sort of Javascripty eh?
  24. PHP 5.3 – Major Deprecations • INI – register_globals –

    safe_mode – magic_quotes. – All of these are deprecated. Depending on these means you need to take a serious look at some habits and correct them.
  25. PHP 5.3 – Major Deprecations • Functions – call_user_method() (use

    call_user_func with array($obj,'method') syntax). – dl() - live loading of extra libraries will only be supported on the CLI. – The entire ereg family of pattern functions. Move to the preg family. – session_register() - use $_SESSION[....] like any other array.
  26. PHP 5.3 – Major Deprecations • Call time pass-by- reference.

    – If a function expects a value and you give it a reference, you're gonna have a bad time. Going to throw warnings.
  27. PHP 5.3 – Major Deprecations • Call time pass-by-value-but-you-specified- reference-in-the-function-declaration.

    – If you try to pass a straight value to a function that expects a reference, you're going to have an even badder time. Fatal error. – This used to be allowed and it made for very poorly structured applications that sounded good at the time. – Think extended extended extended classes in Zend Framework and a straight array() argument gets in the mix.
  28. PHP 5.3 – Other Changes • call_user_func will propagate $this

    inside methods. • trailing slashes removed from SplFileInfo and related classes when asking for path names. • Class magic methods must be made public. • added __callStatic and __invoke • var_dump will list private properties. • Exceptions can be nested – throwing from catch.
  29. PHP 5.3 to 5.4 • Goodbye safe_mode. • Goodbye magic_quotes.

    • Goodbye register_globals. • Goodbye call time PBR. • Hello traits. • Hello short array syntax. $var = [ 'stuff' ]; • Closures propagate $this. • Binary Integer Format Added. 0B0001 • OpenSSL ext now supports AES • Instance dereferencing: – echo (new Test)->Hello(); • Built-in test server. • Functions which return arrays can be dereferenced. – $first_value = returns_an_array()[0];
  30. PHP 5.4 Got-ya • PDO now *requires* MySQL 4.1 •

    htmlentities now returns UTF-8 strings. • E_ALL now includes E_STRICT so you are going to notice if you are doing something bad. • Using an array as a string will still print “Array” but also throw a notice. • Time zones can no longer be set by the TZ system variable. Must be in the INI.
  31. PHP 5.4 to 5.5 • New generators! • New finally!

    • New password_hash tools. • Opcache!!! • SPL Classes: CURLFile, DateTimeImmutable • GD: New imageflip, imagecrop, and webp support
  32. PHP 5.5 - Generators • Create dynamic sources of iteration.

    – … – lol #40 – lol #41 – lol #42
  33. PHP 5.5 - Finally • $ test.php – and now

    finally • $ test.php – caughteded – and now finally
  34. PHP 5.5 – Finally (cont) • “code in finally is

    always executed after try and catch” • not really in the documentation: – except the return in finally will always overrule a return elsewhere.
  35. PHP 5.5 - Password Hash • “The used algorithm, cost

    and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it.” • $2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K – if(password_verify($text,$hash)) { … } else { … }
  36. PHP 5.5 - foreach() • foreach() can now use list()

    to break subarrays apart. • Verify your data beforehand to prevent errors like “not an array” or “invalid index”
  37. PHP 5.5 – Various tweaks • Keywords self::, parent::, and

    static:: are now case insensitive. • The PHP image logos were removed from the internal source code. (lol, finally?) • ClassName::class will return the FQCN. – $class = self::class;
  38. PHP 5.5 Got-ya • Some format arguments for pack() an

    unpack() were changed to be more Perlesque. • Windows XP and Server 2003 support dropped. • The entire old MySQL is deprecated. Use MySQLi or PDO. • preg_replace: the /e (execute) flag is deprecated.
  39. • @bobmajdakjr • [email protected] • Don't forget about Lone Star

    PHP this April PHP Migration Documentation: http://www.php.net/manual/en/doc.changelog.php