See rates for the top Laravel developers in Latin America

Typed Properties Coming to PHP 7.4

Published on by

Typed Properties Coming to PHP 7.4 image

The Typed Properties 2.0 RFC was accepted with a vote of 70 in favor and one no vote. A 2/3 majority is required because typed properties is a language change. The typed property change is a PHP 7.4 proposal.

With the introduction of scalar types and return types, PHP 7 greatly increased the power of PHP’s type system. However, it is currently not possible to declare types for class properties, forcing developers to use getter and setter methods instead to enforce type contracts. This requires unnecessary boilerplate, makes usage less ergonomic and hurts performance. This RFC resolves this issue by introducing support for first-class property type declarations.

The RFC provides an example where the goal is to enforce type-safety:

class User {
/** @var int $id */
private $id;
/** @var string $name */
private $name;
 
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
 
public function getId(): int {
return $this->id;
}
public function setId(int $id): void {
$this->id = $id;
}
 
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
}

With the new accepted RFC, that code could be functionally equivalent as follows “without sacrificing type safety”:

class User {
public int $id;
public string $name;
 
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}

Finally, here’s an example of all the valid types supported in runtime-enforced annotations for typed properties:

class Example {
// All types with the exception of "void" and "callable" are supported
public int $scalarType;
protected ClassName $classType;
private ?ClassName $nullableClassType;
 
// Types are also legal on static properties
public static iterable $staticProp;
 
// Types can also be used with the "var" notation
var bool $flag;
 
// Typed properties may have default values (more below)
public string $str = "foo";
public ?string $nullableStr = null;
 
// The type applies to all properties in one declaration
public float $x, $y;
// equivalent to:
public float $x;
public float $y;
}

It’s clear from the massive 70 votes in favor to 1 vote in opposition that the PHP internals team wants to continue to introduce type features, such as type-safety, into the PHP language.

On the other end of the spectrum, PHP is usable in a completely different paradigm of a fully dynamic language embracing type coercion. However, with the continuation of new type-safety features introduced in PHP 7, it’s unclear how practical it will remain to use a fully dynamic approach to writing PHP applications.

For example, if common libraries like the Symfony components and the de-facto PHP testing framework PHPUnit implement this style of syntax, package consumers working with these libraries are required to follow suit and adapt to the new world that is a very type-focused language.

Some may argue that forcing a type-focused approach is a good thing, and will make programmers write better programs with fewer bugs. Along with this argument, static analysis tools may very well be able to detect runtime errors ahead of time.

At least for me, this type of discussion has rarely been around concrete evidence that type-safety makes one’s code better, and more around hypothetical scenarios that make it hard to discern if type-safety makes code “better.”

I don’t know if the division of programming styles will widen between dynamic and strongly typed programmers. To me, it seems inevitable that the language features will eschew dynamic programming styles due to the rising preference of developers wanting a strongly typed language.

What does that mean for those wanting to program without types? Only time will tell, but I predict that at least a small majority of programmers wishing the language to stay with its dynamic roots will look to other languages.

Learn More

To learn more the rfc:typed_properties_v2 proposal is the best source of accurate information at this point. An implementation is already under way for typed properties.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
NativePHP logo

NativePHP

Build rich mobile apps across iOS and Android from a single Laravel codebase. This changes everything!

NativePHP
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Reset Rate Limits Dynamically with Laravel's clear Method image

Reset Rate Limits Dynamically with Laravel's clear Method

Read article
Manipulate Image URLs in Laravel with the Image Transform Package image

Manipulate Image URLs in Laravel with the Image Transform Package

Read article
Handle Nested Arrays Elegantly with Laravel's fluent() Helper image

Handle Nested Arrays Elegantly with Laravel's fluent() Helper

Read article
Laravel 12.19 Adds a useEloquentBuilder Attribute, a FailOnException Queue Middleware, and More image

Laravel 12.19 Adds a useEloquentBuilder Attribute, a FailOnException Queue Middleware, and More

Read article
Test Deferred Operations Easily with Laravel's withoutDefer Helper image

Test Deferred Operations Easily with Laravel's withoutDefer Helper

Read article
Larallow is a Permissions Package With Support for Scopes image

Larallow is a Permissions Package With Support for Scopes

Read article