xp-lang/php-switch-expression

Switch expression for PHP


Keywords
module, language, xp
License
BSD-3-Clause

Documentation

Switch expression for PHP

Build Status on TravisCI XP Framework Module BSD Licence Requires PHP 7.0+ Latest Stable Version

Plugin for the XP Compiler which adds switch expressions to the PHP language.

Example

use util\Date;
use lang\IllegalArgumentException;

public function serialize($arg) {
  return switch ($arg) {
    case true      => 'b:1;';
    case false     => 'b:0;';
    case null      => 'N;';
    case is int    => 'i:'.$arg.';';
    case is string => 's:'.strlen($arg).'"'.$arg.'";';
    case is Date   => '@:'.$arg->getTime().';';
    default        => throw new IllegalArgumentException('Unhandled '.typeof($arg));
  };
}

Installation

After installing the XP Compiler into your project, also include this plugin.

$ composer require xp-framework/compiler
# ...

$ composer require xp-lang/php-switch-expression
# ...

No further action is required.

See also