Page MenuHomePhabricator

[Session] Cool new things in PHP
Closed, ResolvedPublic

Description


I could imagine two styles of doing this collaboratively: collecting everyone’s ideas for things to mention in advance (and then have one person putting together a presentation and holding it), or having people suggest things during the session.

Incomplete list of things to mention:

  • yield (not very new, but still useful, especially for test case data providers)
  • static types (fka type hints) – just a very quick overview: param types, return types, property types
  • arrow functions
  • ... operator: in function declarations, function calls, arrays
  • named arguments
  • attributes
  • constructor property promotion

Modern PHP (blog post) also seems like a useful resource.

Event Timeline

??= and <-> are two underappreciated, somewhat newish operators.

Although it will be a while until MediaWiki can use it, fibers also look interesting.

Slides will be here (but so far I’ve just dumped everything into a TODO slide, will expand later); feel free to comment there: https://docs.google.com/presentation/d/1LkyCXbBA3O2M1XKkWKVuq8RpwqHPICzWJ2FOleXQXcc/edit

After the session I’ll also put the finished slides on Commons, of course.

Below you may find the link attached that redirects the user to the corresponding Etherpad: https://etherpad.wikimedia.org/p/wmh2023-Cool_new_things_in_PHP

My presentation slides are done now; I did a test run and needed ca. 27 minutes, so I might have to speed up a little bit (or cut out some waffling from what I was saying without removing any slides) to ensure it stays within the time limit.

Attention interested attendants: this session has been rescheduled, to 11:30 in the small hacking corner room (in parallel with the first half of T333853: [Session] Self-hosting ML models on Cloud Services).

The session still takes place at the same time, and in the “secondary” presentations room, but that room is now the second floor of Innovathens. Looking forward to seeing y’all!

Closing the task as the session is done. Note that the slide deck PDF on Commons includes additional slides between the ones I showed live, more or less summarizing what I said out loud; some people also took notes.

Notes from etherpad:

Cool new things in PHP

Date & time: Sunday, May 21st at 11:30 am EEST / 8:30 am UTC

Relevant links

Notes

Already available (PHP 7.4)

  • yield

especially for data providers
*it's good to add static for data providers

  • ... operator

for arbritary arguments
merge arrays into another array

  • array destructuring

replacing list keyword

  • static types/ type declarations

php now can infer the type, no need to type check inside the func
you don't need the comment anymore

  • strict types

in some cases php casts parameters to the type expected
declare( strict_types = 1 ) to avoid these suprises

  • arrow functions

=> to represent the return type
in anon funcs it captures all the variables automatically
but can't capture by reference

  • new operators

?? is like the isset() test
<=> it's called a spaceship
returning -1, 0, 1 depending on if a number is smaller, equal or bigger than another

  • new functions

Coming to PHP 8.0

  • Union types

part of static types hinting we have just seen, makes it more flexible
ex: string|false will be something valid as a type, can return either of the types

  • never return type

the family of die funcs will never return
it exits or throws, if it returns PHP will throw a type error

  • named arguments

jump ahead in the parameter list by naming the parameters directly you want to use
e.g. file_get_contents( flags: JSON_THROWN_ON_ERROR );
if you started using a named parameters you probably cannot continue with unnamed afterwards

  • ?->

chained optional arrow function so it breaks the chain response if needed

  • Attributes

can be used already because they translate to a comment in older php versions
???

  • Constructor property promotion

private keyword on the constructor it becomes a prop of the class

  • match expressions

match a list of inputs to a certain output to replace a switch statement doing the same
strict comparison in contrast to switch statement which is a loose comparison

  • enums

enums are just great, no more raw ints
can even have underlying values

Questions

  • Enums can have underlying values, so can you have bitflags? no???
  • Can enums be used with union types? You can pass only one of the cases, maybe?