Skip to main content

Variable Types and Return Types

Premium
4 min read

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 of our courses? (30 h 41 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.