Only until March 18th: coupon LARAVEL12 for 40% off Yearly/Lifetime membership!

Read more here
Courses

PHP for Laravel Developers

Variable Types and Return Types

Summary of this lesson:
- Understand PHP type hinting mechanisms
- Learn about union and nullable types
- Explore constructor property promotion
- Examine type conversion and checking techniques

Have you noticed that Laravel 10 came with a big skeleton rewriting to use PHP types everywhere - for parameters, properties, variables, and method returns. But let's explore what are the possible types and their syntax details.

Let's look at two code samples:

Without types:

class UserService {
 
public $data;
 
public function processData($user)
{
// ...
}
}

With types:

use App\Models\User;
 
class UserService {
 
public array $data;
 
public function processData(User $user): User
{
// ...
}
}

The second example is more readable, right? If a developer opens the first example for the first time, he would have...

The full lesson is only for Premium Members.
Want to access all 16 lessons of this course? (52 min read)

You also get:

  • 71 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord