Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
PHP
(section)
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!
==== 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" />
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)