Skip to content

Commit fdfe38e

Browse files
committed
-- Add an example for Nullsafe Operator
1 parent f0e6e6e commit fdfe38e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
- [PHP For Beginners](challenge-001/README.md)
44
- [Object-Oriented Principles in PHP](challenge-002/README.md)
5+
- [PHP 8 Crash Course](challenge-003/README.md)

challenge-003/02.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class User {
4+
public function profile()
5+
{
6+
// return new Profile;
7+
return null;
8+
}
9+
}
10+
11+
class Profile {
12+
public function employment()
13+
{
14+
return 'web developer';
15+
}
16+
}
17+
18+
$user = new User;
19+
20+
// $profile = $user->profile();
21+
22+
// if ($profile) {
23+
// echo $profile->employment();
24+
// }
25+
26+
// var_dump($user->profile()?->employment()); // NULL
27+
echo $user->profile()?->employment() ?? 'Not provided';

challenge-003/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# PHP 8 Crash Course
2+
3+
- PHP 8 is now officially available. As part of this new release, not only will you benefit from performance improvements - thanks to the new JIT compiler - but you'll also enjoy useful new operators and types, attributes, match expressions, and so much more.
4+
5+
- If you're intrigued, hop in and we'll review everything you need to know.
6+
7+
## 01. How to Install PHP 8
8+
9+
- About
10+
11+
Before we can play around, of course we must first download PHP 8. At the time of this writing, PHP 8 is not yet officially out. As such, we'll tap a custom [Homebrew](http://brew.sh/) formula to make the installation process as painless as possible. Alternatively, you might consider using Docker or Vagrant to create an isolated environment for testing PHP 8.
12+
13+
## 02. The Nullsafe Operator
14+
15+
- About
16+
17+
First up on the agenda is the new [Nullsafe operator](https://wiki.php.net/rfc/nullsafe_operator). This operator - represented as a question mark - allows you to call a method on the result of any expression if it does not evaluate to null. It sounds confusing, but it really isn't. Let's have a look.
18+
19+
## 03. Match Expressions
20+
21+
- About
22+
23+
Switch statements in PHP are useful, yet clunky. Think of the new [match expression](https://wiki.php.net/rfc/match_expression_v2) in PHP 8 as an improved switch. It's far more terse and flexible than its counterpart.
24+
25+
## 04. Constructor Property Promotion
26+
27+
- About
28+
29+
Next up is [constructor property promotion](https://wiki.php.net/rfc/constructor_promotion), which allows you to remove much of the tedious class initialization boilerplate code that you likely write for every any that accepts a dependency.
30+
31+
## 05. $object::class
32+
33+
- About
34+
35+
This next one is a small, but useful addition to PHP 8 that received [unanimous support](https://wiki.php.net/rfc/class_name_literal_on_object) during the voting stage. You now have the ability to use `::class` directly on an object. The result will be functionally identical to the result of `get_class()`. In PHP 7 and below, this functionality was limited to the class, itself.
36+
37+
## 06. Named Parameters
38+
39+
- About
40+
41+
Next up, we have [named parameters](https://wiki.php.net/rfc/named_params). This new PHP 8 feature allows you to pass function arguments according to, not their order, but the parameter name, itself. Let's discuss the pros and cons of adopting named parameters/arguments in your own projects.
42+
43+
## 07. New String Helpers
44+
45+
- About
46+
47+
It took a global pandemic for PHP to finally add a `str_contains` helper function, but it's finally here (along with a few others). In this episode, we'll review `str_contains()`, `str_starts_with()`, and `str_ends_with()`.
48+
49+
## 08. Weak Maps
50+
51+
- About
52+
53+
Weak maps are effectively key value stores that allow for garbage collection. You won't reach for these often, but they're nonetheless an important tool to have in your belt.
54+
55+
## 09. Union and Pseudo Types
56+
57+
- About
58+
59+
Next up, we'll discuss PHP 8's support for union types, as well as a new catch-all `mixed` pseudo-type. We can now - without resorting to docblocks - specify that a method parameter may accept multiple types.

0 commit comments

Comments
 (0)