The PHP Pattern Matching RFC introduces a new syntax to PHP that allows developers to match variables against complex data structures, similar to how regular expressions work for strings.
This feature, inspired by languages like Python, Haskell, and Rust, aims to make code more readable and concise when dealing with conditional structures.
The RFC was authored by Larry Garfield and Ilija Tovilo and is part of the larger Algebraic Data Types Epic, intended to eventually support full Algebraic Data Types in PHP.
Table of contents:
The RFC proposes the introduction of the is
keyword, which evaluates to a boolean when used to match a variable against a pattern. This core functionality includes several basic types of patterns:
$var is string;
$var is int|float;
$var is ?array;
$var is "foo";
$var is 5;
$var is 3|5|null;
$p is Point{ x: 3 };
$p is Point{ y: 3 }|null;
$list is [1, 2, 3, 4];
$list is [1, 2, 3, ...];
$assoc is ['a' => 'A', 'b' => 'B'];
The RFC also discusses additional patterns that are beneficial but not mandatory for the initial release:
$foo is @($bar);
$p is Point { x: 3, y: $y is 5|6 };
$foo is 0..=10;
$name is /\w{3,}/;
Pattern matching can be used with the match()
structure to perform pattern matches rather than identity comparisons. This makes the match structure more powerful and flexible.
$result = match ($somevar) is {
Foo => 'foo',
Bar => 'bar',
Baz|Beep => 'baz',
};
Pattern matching can extract values from larger structures into variables, making it easier to work with complex data.
if ($p is Point {x: 3, y: $y} ) {
print "x is 3 and y is $y.";
}
The RFC outlines several additional patterns and features that could be added in the future:
$ints is array<int>;
$arr is ['a' => string, ?'b' => string, ...];
The new is
keyword introduces a minor backward compatibility break, as it conflicts with any user-defined global constant named is
. The RFC proposes implementation for PHP 8.next (likely PHP 8.4).
The PHP Pattern Matching RFC represents a significant enhancement to PHP's capabilities, making it easier to work with complex data structures in a concise and readable manner.
This feature aligns PHP with other modern programming languages and sets the stage for future enhancements, including Algebraic Data Types. The feedback from the community will shape the final implementation and determine which patterns are included in the initial release.
For further details and updates, refer to the official PHP RFC page.
Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.
Get 1 month free with yearly plan during checkout.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.