Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
PHP
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== PHP 8 === PHP 8 was released on 26 November 2020, and is currently the second-most used PHP major version. PHP 8 is a major version and has breaking changes from previous versions.<ref name="Brent">{{cite web |author=Brent |title=What's new in PHP 8 |url=https://stitcher.io/blog/new-in-php-8 |website=Stitcher |access-date=22 September 2020}}</ref><ref name="PHP">{{cite web |title=PHP 8 Released|url=https://www.php.net/releases/8.0/en.php |website=PHP |access-date=27 November 2020}}</ref> New features and notable changes include: ==== Just-in-time compilation ==== [[Just-in-time compilation]] is supported in PHP 8.<ref name="wiki.php.net-2" /> PHP 8's [[just-in-time compilation|JIT compiler]] can provide substantial performance improvements for some use cases,<ref name="Brent-2">{{cite web |author=Brent |title=PHP 8: JIT performance in real-life web applications |url=https://stitcher.io/blog/jit-in-real-life-web-applications |website=Stitcher.io |access-date=4 October 2020}}</ref><ref>{{cite web |last1=Rethams |first1=Derick |title=PHP 8: A Quick Look at JIT |url=https://derickrethans.nl/a-quick-look-at-jit.html}}</ref> while (then PHP) developer Nikita Popov stated that the performance improvements for most websites will be less substantial than the upgrade from PHP 5 to PHP 7.<ref name="Popov-2020" /> Substantial improvements are expected more for mathematical-type operations than for common web-development use cases.<ref name="Popov-2020">{{cite web |last1=Popov |first1=Nikita |title="What's new in PHP 8.0?" Nikita Popov |date=13 July 2020 |url=https://www.youtube.com/watch?v=NbBRXwu1Md8 |archive-url=https://ghostarchive.org/varchive/youtube/20211211/NbBRXwu1Md8| archive-date=2021-12-11 |url-status=live|publisher=PHP fwdays |access-date=4 October 2020}}{{cbignore}}</ref> Additionally, the JIT compiler provides the future potential to move some code from C to PHP, due to the performance improvements for some use cases.<ref name="Daniele-2020">{{cite web |last1=Daniele |first1=Carlo |title=What's New in PHP 8 (Features, Improvements, and the JIT Compiler) |url=https://kinsta.com/blog/php-8/ |website=Kinsta |date=25 May 2020 |access-date=24 December 2020}}</ref> ==== Addition of the match expression ==== {{Main|PHP syntax and semantics#Match expression}} PHP 8 introduced the {{code|match}} expression.<ref name="Redmond-2020">{{cite web |last1=Redmond |first1=Paul |title=Match Expression is Coming to PHP 8 |url=https://laravel-news.com/match-expression-php-8 |website=Laravel News |date=15 July 2020 |access-date=4 October 2020}}</ref> The match expression is conceptually similar to a {{code|switch}} statement and is more compact for some use cases.<ref name="PHP Watch">{{cite web |title=PHP 8.0: Match Expressions |url=https://php.watch/versions/8.0/match-expression |website=PHP Watch |access-date=4 October 2020}}</ref> Because {{code|match}} is an expression, its result can be assigned to a variable or returned from a function.<ref>{{cite web |last1=Barnes |first1=Eric |title=PHP 8 is now Released! |url=https://laravel-news.com/php-8 |website=Laravel News |date=27 November 2020 |access-date=24 December 2020 |ref=laravel-news-php-8-released}}</ref> ==== Type changes and additions ==== PHP 8 introduced union types, a new {{code|static}} return type, and a new {{code|mixed}} type.<ref name="Brent" /> "Attributes", often referred to as "annotations" in other programming languages, were added in PHP 8, which allow metadata to be added to classes.<ref name="Brent" /> {{code|throw}} was changed from being a statement to being an expression.<ref name="wiki.php.net-4" /> This allows exceptions to be thrown in places that were not previously possible.<ref name="Brent" /> ==== Syntax changes and additions ==== PHP 8 includes changes to allow alternate, more concise, or more consistent syntaxes in a number of scenarios. For example, the nullsafe operator is similar to the [[null coalescing operator]] {{code|??}}, but used when calling methods.<ref name="wiki.php.net" /> The following code snippet will not throw an error if {{code|getBirthday()}} returns null: <syntaxhighlight lang="php"> $human_readable_date = $user->getBirthday()?->diffForHumans(); </syntaxhighlight> Constructor property promotion has been added as "[[syntactic sugar]]," allowing class properties to be set automatically when parameters are passed into a class [[Constructor (object-oriented programming)|constructor]].<ref name="Brent" /> This reduces the amount of [[boilerplate code]] that must be written.<ref>{{cite web |last1=Roose |first1=Brent |title=PHP 8: Constructor property promotion |url=https://stitcher.io/blog/constructor-promotion-in-php-8 |access-date=30 April 2024}}</ref> Other minor changes include support for use of {{code|::class}} on objects, which serves as an alternative for the use of {{code|get_class()}};<ref name="Brent" /> non-capturing catches in try-catch blocks; variable syntax tweaks to resolve inconsistencies; support for named arguments; and support for trailing commas in parameter lists, which adds consistency with support for trailing commas in other contexts, such as in arrays.<ref name="PHP" /> ==== Standard library changes and additions ==== * Weak maps were added in PHP 8. A {{code|WeakMap}} holds references to objects, but these references do not prevent such objects from being [[Garbage collection (computer science)|garbage collected]].<ref name="wiki.php.net-3" /> This can provide performance improvements in scenarios where data is being [[Cache (computing)|cached]]; this is of particular relevance for [[object–relational mapping]]s (ORM).<ref name="Brent" /> * Various adjustments to interfaces, such as adding support for creating {{code|DateTime}} objects from interfaces, and the addition of a {{code|Stringable}} interface that can be used for type hinting.<ref name="Brent" /> * Various new functions including {{code|lang=php|str_contains()}}, {{code|lang=php|str_starts_with()}}, and {{code|lang=php|str_ends_with()}};<ref name="Merchant-2020">{{cite web |last1=Merchant |first1=Amit |title=These new string functions are coming in PHP 8 |url=https://www.amitmerchant.com/new-string-functions-php8/ |website=Amit Merchant |date=13 June 2020 |access-date=4 October 2020}}</ref> {{code|lang=php|fdiv()}}; {{code|lang=php|get_debug_type()}}; and {{code|lang=php|get_resource_id()}}<ref name="Brent" /> * Object implementation of {{code|lang=php|token_get_all()}}<ref name="Brent" /> ==== Additional changes ==== * Type annotations were also added into PHP's C source code itself to allow internal functions and methods to have "complete type information in reflection."<ref>{{cite web |last1=Popov |first1=Nikita |title=Call for participation: Annotating internal function argument and return types |url=https://externals.io/message/106522 |website=Externals |access-date=19 November 2020}}</ref> * Inheritance with private methods<ref name="Brent" /> * Abstract methods in traits improvements<ref name="Brent" />
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
PHP
(section)
Add topic