DeveloperJoy Logo DeveloperJoy

PHP Pattern Matching RFC

June 24, 2024
PHP Pattern Matching RFC

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.

Core Functionality

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:

  • Basic Type Matching: Allows matching against simple types or combinations of types.
    $var is string;
    $var is int|float;
    $var is ?array;
  • Literal Patterns: Matches variables against specific values.
    $var is "foo";
    $var is 5;
    $var is 3|5|null;
  • Object Patterns: Matches properties of objects.
    $p is Point{ x: 3 };
    $p is Point{ y: 3 }|null;
  • Array Sequence Patterns: Matches elements of arrays in specific sequences.
    $list is [1, 2, 3, 4];
    $list is [1, 2, 3, ...];
  • Associative Array Patterns: Matches key-value pairs in arrays, where order does not matter.
    $assoc is ['a' => 'A', 'b' => 'B'];

Extended Patterns

The RFC also discusses additional patterns that are beneficial but not mandatory for the initial release:

  • Variable Reference Expressions: Allows the use of variables from the current scope within patterns.
    $foo is @($bar);
  • Nested Patterns for Captured Variables: Enables more complex pattern matching with nested structures.
    $p is Point { x: 3, y: $y is 5|6 };
  • Range Patterns: Matches numeric variables within specified ranges.
    $foo is 0..=10;
  • Regex Patterns: Matches string variables against regular expressions.
    $name is /\w{3,}/;

Match() Enhancement

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',

Variable Binding

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.";

Optional and Future Patterns

The RFC outlines several additional patterns and features that could be added in the future:

  • Array Application Pattern: Applies a pattern across all elements of an array.
    $ints is array<int>;
  • Optional Array Keys: Allows specifying that certain keys in an associative array are optional.
    $arr is ['a' => string, ?'b' => string, ...];

Backward Compatibility and Implementation

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 (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.

← Go back to the blog

Work with me

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.

Technical Advisor

$ 3 ,995


What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995


Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.