What's new in PHP 8.0

2021-09-09 68浏览

  • 1. What's new in PHP 8.0? Nikita Popov @ PhpConChina 2020
  • 2.
  • 3. PHP 8.0 ● Planned release date: November 26th ● Large number of new features ● Backwards-compatibility breaks
  • 4. Just-In-Time (JIT) Compiler ● ● Compiles PHP code to x86 machine code Performance improvement depends on type of code
  • 5. Just-In-Time (JIT) Compiler ● ● Compiles PHP code to x86 machine code Performance improvement depends on type of code – WordPress: ~5% improvement – PHP-Parser: 2x faster
  • 6. Just-In-Time (JIT) Compiler ● Compiles PHP code to x86 machine code ● Part of opcache: – opcache.jit=on – opcache.jit_buffer_size=128M
  • 7. Attributes
  • 8. Attributes
  • 9. Attributes
  • 10. Attributes
  • 11. Attributes
  • 12. Attributes getAttributes() as $attr) { var_dump($attr->getName()); // => "Doctrine\ORM\Attributes\Column" var_dump($attr->getArguments()); // => ["integer"] } var_dump($attr->newInstance()); // object(Doctrine\ORM\Attributes\Column)
  • 13. Attributes getAttributes() as $attr) { var_dump($attr->getName()); // => "Doctrine\ORM\Attributes\Column" var_dump($attr->getArguments()); // => ["integer"] } var_dump($attr->newInstance()); // object(Doctrine\ORM\Attributes\Column) Attribute validation happens HERE.
  • 14. Constructor Promotion x = $x; $this->y = $y; $this->z = $z; }
  • 15. Constructor Promotion
  • 16. Constructor Promotion
  • 17. Named Arguments
  • 18. Named Arguments
  • 19. Named Arguments
  • 20. Named Arguments
  • 21. Named Arguments
  • 22. Named Arguments
  • 23. Named Arguments
  • 24. Named Arguments
  • 25. Named Arguments
  • 26. Named Arguments 2.0, "y" => 3.1, "z" => 4.2]; new Point(...$array);
  • 27. Named Arguments 3, "y" => 4]
  • 28. Named Arguments method(name_a: 42); Names not the same
  • 29. Union Types number = $number; } /** @return int|float */ public function getNumber() { return $this->number; } }
  • 30. Union Types number = $number; } public function getNumber(): int|float { return $this->number; } }
  • 31. Union Types
  • 32. Union Types
  • 33. Union Types
  • 34. Union Types ● Tricky interaction with "weak types" ● Type must be part of union, or... ● Scalars are coerced to int, float, string, bool, in order of preference
  • 35. Union Types
  • 36. Union Types
  • 37. Mixed Type ● Distinguishes between: – Type is missing because I didn't add one yet – This function really does accept any value
  • 38. Mixed Type
  • 39. Mixed Type ( array $arg, callable(R, V): R $callback, R $initial = null ): R {}
  • 40. Mixed Type ( array $arg, callable(R, V): R $callback, R $initial = null ): R {} // Back down to earth: function array_reduce( array $arg, callable $callback, mixed $initial = null ): mixed {}
  • 41. Mixed Type
  • 42. Mixed Type
  • 43. Mixed Type
  • 44. Static Return Type
  • 45. Static Return Type
  • 46. Static Return Type whatever = $whatever; return $clone; } }
  • 47. Static Return Type
  • 48. Match Expression
  • 49. Match Expression $a + $b, '-' => $a - $b, '*' => $a * $b, default => throw new UnsupportedOperator($operator); };
  • 50. Match Expression $a + $b, '-' => $a - $b, '*' => $a * $b, default => throw new UnsupportedOperator($operator); };
  • 51. Match Expression $a + $b, '-' => $a - $b, '*' => $a * $b, default => throw new UnsupportedOperator($operator), }; Each match clause is an expression ("throw" is an expression now)
  • 52. Match Expression $a + $b, '-' => $a - $b, '*' => $a * $b, }; } // Match is exhaustive: evalOp('/', 10, 2); // UnhandledMatchError
  • 53. Match Expression $a + $b, '-' => $a - $b, '*' => $a * $b, }; } // Match compares using ===, not ==. evalOp(true, 10, 2); // UnhandledMatchError
  • 54. Nullsafe Operator getUser()->name : null; // Same as: $name = $session?->getUser()->name;
  • 55. Nullsafe Operator getUser()?->name; // Approximately same as: $name = null; if ($session !== null) { $user = $session->getUser(); if ($user !== null) { $name = $user->name; } }
  • 56. Other Features ● catch (Exception) without variable ● $object::class ● str_contains(), str_starts_with(), str_ends_with() ● get_debug_type() ● Stable sorting ● WeakMap
  • 57. Backwards Compatibility Breaks ● ● Functionality deprecated before PHP 8.0 has been removed! Full list: https://github.com/php/php-src/blob/master/UPGRADING
  • 58. Number to String Comparison
  • 59. Number to String Comparison
  • 60. Number to String Comparison Comparison | Before | After ------------------------------ 0 == "0" | true | true 0 == "0.0" | true | true 0 == "foo" | true | false 0 == "" | true | false 42 == " 42" | true | true 42 == "42foo" | true | false
  • 61. Resource To Object Migration ● ● Long term goal: Convert all resources to objects Objects are type-safe and have much better internal support
  • 62. Resource To Object Migration ● ● ● Long term goal: Convert all resources to objects Objects are type-safe and have much better internal support Using "opaque objects" – Actual object-oriented APIs may be added later
  • 63. Resource To Object Migration ● CurlHandle, CurlMultiHandle, CurlShareHandle ● EnchantBroker, EnchantDictionary ● GdImage ● InflateContext, DeflateContext ● OpenSSLCertificate, OpenSSLCertificateSigningRequest, OpenSSLAsymmetricKey ● Shmop ● Socket, AddressInfo ● SysvMessageQueue, SysvSemaphore, SysvSharedMemory ● XmlParser ● XmlWriter (already had an OO API)
  • 64. Resource To Object Migration
  • 65. Resource To Object Migration
  • 66. Resource To Object Migration
  • 67. Warning → Error exception ● Many warnings converted to Error exceptions – TypeError – ValueError
  • 68. Warning → Error exception ● ● Only allowed for error conditions that imply programmer error It makes no sense to "handle" the error, code needs to be fixed instead
  • 69. Warning → Error exception
  • 70. Warning → Error exception
  • 71. Warning → Error exception
  • 72. Warning → Error exception
  • 73. Warning → Error exception
  • 74. Warning → Error exception
  • 75. PHP Stubs ● ● PHP stub files specify function signatures for internal functions/methods Used to generate C code for function registration
  • 76. PHP Stubs
  • 77. PHP Stubs
  • 78. PHP Stubs ● Data available through Reflection: – ReflectionFunction::getReturnType() – ReflectionParameter::getType() – ReflectionParameter::getDefaultValue()
  • 79. PHP Stubs
  • 80. PHP Stubs
  • 81. PHP Stubs
  • 82. 3v4l.org
  • 83. Travis CI php: - nightly install: - | if [ $TRAVIS_PHP_VERSION = 'nightly' ]; then composer install --ignore-platform-reqs; else composer install; fi Some libraries are not formally compatible with PHP 8 (yet)
  • 84. Thank You!