PHP Typing
约 1 分钟阅读
- 不支持 generics
<?php
// PHP 7.4+
declare(strict_types=1)
class MyObject {
// typed properties by phpdoc
/** @var Task[] $tasks */
private array $tasks;
}- 不支持
void,callable
支持类型
string,int,bool- PHP 7.0+float,array,iterable,objectstdClassself,parentmixed- Nullable types - PHP 7.1+
?string,?int,?bool
- Typed Properties - PHP 7.4
null,false,true- PHP 8.0+- Union Types - PHP 8.0+
string|nullstring|falsestring|false|null
- DNF 类型 - PHP 8.2+
- DNFWikipediaDisjunctive normal form - Disjunctive Normal Form - 析取范式
A|B|CA|B|(C&D)(A&B&C)|null(JSONResponse&SuccessResponse)|HTMLResponse|string->A&(B|C)(A&B)|(A&C)->A|(B&(C|D))A|(B&C)|B&D)
- Typed array
int[]- PHP 8.3 - Typed Class Constants - PHP 8.3
参考
关联信息
反向链接、本文链接的其他页面和外部资料。
References
Wikipedia1 条
- Disjunctive normal formWikipedia:Disjunctive normal form
其他外链4 条
- phpstan.org/writing-php-code/phpdoc-typesphpstan.org/writing-php-code/phpdoc-types
- wiki.php.net/rfc/typed_properties_v2wiki.php.net/rfc/typed_properties_v2
- wiki.php.net/rfc/typed-propertieswiki.php.net/rfc/typed-properties
- www.php.net/manual/zh/language.types.declarations.phpwww.php.net/manual/zh/language.types.declarations.php