Home > headlines > PHP is about to introduce Union Types, the vote has passed!

PHP is about to introduce Union Types, the vote has passed!

爱喝马黛茶的安东尼
Release: 2019-11-15 17:58:32
forward
4422 people have browsed it

PHP is about to introduce Union Types, the vote has passed!

The vote on whether to introduce Union Types in PHP 8 ended recently. The results showed that 61 members of the PHP development team voted in favor and 5 voted against.

PHP is about to introduce Union Types, the vote has passed!

PHP is about to introduce Union Types, the vote has passed!

also noticed that Brother Bird voted against the vote~

Therefore, according to the voting results, the official has confirmed Union Types 2.0 will be introduced in PHP 8.

PHP is about to introduce Union Types, the vote has passed!

Detailed discussion about Union Typescan be viewed on GitHub. Let’s take a brief look at Union Types.

According to the official introduction, Union Types (Union Types) support receiving multiple values ​​of different types, not just a single type. PHP currently supports two special union types:

·Type or null, using the special Type syntax

·array or Traversable, making the special iterable type

However, PHP does not currently support arbitrary union types. If you want to use it, you need the help of phpdoc comments. The example is as follows:

<pre style="margin: 0px; padding: 0px; font-family: "Courier New" !important; font-size: 14px; line-height: 
1.5em; white-space: pre-wrap; overflow-wrap: break-word;">class Number { /**
* @var int|float number; /**
* @param int|float number) { number;
} /**
* @return int|float */
public function getNumber () { return $this->number;
}
}
Copy after login

According to the results of statistics, the use of union types is very common in the open source ecosystem and PHP's own standard library. Officially, if PHP can support union types, it will allow us to migrate more type information from phpdoc to function signatures, which has the following common advantages:

· Types are actually enforced so errors can be caught early.

· Because they are mandatory, type information is less likely to become outdated or miss edge cases.

·The type will be checked during the inheritance process to implement the Liskov Substitution Principle

· Type information can be obtained through reflection.

·The syntax is simpler than phpdoc.

After generics, union types can be said to be the biggest "gap" in the current type declaration system.

****Proposal****

Union types use T1T2... syntax and can be used in all accepted types:

<pre style="margin: 0px; padding: 0px; font-family: "Courier New" !important; font-size: 14px; line-height: 
1.5em; white-space: pre-wrap; overflow-wrap: break-word;">class Number { private int|float number): void { number;
} public function getNumber (): int|float { return $this->number;
}
}
Copy after login

Supported types

Union types support all types currently supported by PHP: null types, nullable union types, false pseudo-types, repeated and redundant types.

****Type syntax****

Except for the special void type, PHP's type syntax can now be described by the following syntax:

<pre style="margin: 0px; padding: 0px; font-family: "Courier New" !important; font-size: 14px; line-height: 
1.5em; white-space: pre-wrap; overflow-wrap: break-word;">type: simple_type | "?" simple_type | union_type
;
union_type: simple_type "|" simple_type | union_type "|" simple_type
;
simple_type: "false" # only legal in unions
| "null" # only legal in unions
| "bool"
| "int"
| "float"
| "string"
| "array"
| "object"
| "iterable"
| "callable" # not legal in property types
| "self"
| "parent"
| namespaced_name
;
Copy after login
1 0

PHP Chinese website , a large number of free PHP introductory tutorials, welcome to learn online!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template