8 things you don't know about PHP 8 đŸ”„

Thread đŸ§”
1 / Nullsafe operator

The most common error I have is...
"Trying to get property 'someProperty' of non-object"

Now it's solved with nullsafe operator đŸ”„

It reduces sooo much all these boilerplate checks!
2 / Named arguments

Ever had a hard time remembering the order of function arguments?

Don't bother anymore.

Just provide names of the arguments in any order. You can even skip arguments with a default value.

Awesome 😎
3 / Match expression.

To reduce the boilerplate code in the switch statement, PHP 8 introduces the match expression.

No need for "break", many "case", "return".

Simple, self-explaining, easy-to-use đŸ’Ș
4 / Constructor property promotion

The common task of constructors is to pass arguments to class properties. Kind of initialization of the object.

Looks like a good candidate for removing boilerplate code.

So... now you can use constructor property promotion 😎
5 / Mixed type

Yes, yes, we know that mixed type causes mixed feelings.

Some people argue that strict typing is better.

But we're in the PHP world and "mixed" is a new type.

PHP types:

- array
- bool
- callable
- int
- float
- null
- object
-resource
- string
6 / Throw expression

Throw is now not a statement but an expression, which allows us to use it in many new places.

A small, yet convenient feature 😊
7 / Allowing ::class on objects

If we needed to get the class of an object, we used get_class($object).

Now it's possible to directly use $object::class.

Nice.
8 / New string functions

Few handful string functions were added:

str_contains()
str_starts_with()
str_ends_with()

Instead of ugly strpos()
As you can see, there are MANY new features in PHP 8.

I like pretty much null nullsafe operator & named arguments.

What's your favorite?
You can follow @vponamariov.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.