Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save masakielastic/2fa2e6efb37c7adc91c1 to your computer and use it in GitHub Desktop.

Select an option

Save masakielastic/2fa2e6efb37c7adc91c1 to your computer and use it in GitHub Desktop.
PHP RFC: Scalar Type Declarations (https://wiki.php.net/rfc/scalar_type_hints_v5) の下翻訳。続きは http://qiita.com/masakielastic/items/d0958f343c8f19365fa8 を参照。

要約

この RFC は4種類のスカラー型、int、float、string と bool の新しい型宣言の追加を提案します。これらの型宣言は PHP 関数が使う既存のメカニズムと同じようにふるまいます。

この RFC はファイル単位でオプションの新しいディレクティブである declare(strict_types=1); をさらに追加することを提案します。このディレクティブによって、エクステンションとビルトインの PHP 関数を含む、すべての関数呼び出しと戻り値の宣言は "strict" なスカラー型の宣言に対して型チェックがなされます。さらに、このディレクティブを伴うエクステンションとビルトインの PHP 関数の呼び出しによって、パラメーター解析が失敗した段階において、E_RECOVERABLE_ERROR が生成され、これらの関数とユーザーランドの型宣言との協調がもたらされます。

これら2つの機能によって、PHP のプログラムは正しく動き、読み書きの前提知識を減らすことができます (self-documenting)。

詳細

スカラー型の宣言

新しい予約語は追加しません。intfloatstringbool の名前は型宣言の用途に許可され、(use と class_alias も含めて) クラス/インターフェース/トレイトの名前に使うことが禁止されます。

内部において新しいユーザーランドのスカラー型の宣言は Fast Parameter Parsing API 関数を呼び出すことで実装されます。

strict_types declare() ディレクティブ

weak な型チェックのふるまい

strict な型チェックのふるまい

strict モードにおけるエラーハンドラのふるまい

Background and Rationale

History

Weak typing and strict typing

Why both?

Type declaration choices

Discussion Points

This Proposal Is A Compromise

Internal Functions Like ceil() Return Unexpected Types

"37" Should Be Accepted For int Types

Integers Should Be Accepted For Strict float Arguments

Weak Should Error On "10 Birds" Style-Strings Passed To Int Parameters

Int -> Float Conversion Isn't Lossless

Int->Float Exception Makes Strict Mode "Flawed"

Static Analysis Is Possible With Weak Declarations

Errors Should Use Exceptions Instead Of Recoverable Errors

Nullable And Union Types

There Should Be A numeric Type

Internal Functions Should "Opt-In" To Typing

Why Not Use "use strict" Instead Of declare()

Why Not Allow Block-Mode For Declare

Internal Functions Do Not Have Declared Return Types

Why Not Add Support For Null?

Why Not Add Support For MIXED?

Why Not Add An INI Setting For Default Mode

Type Aliases Should Not Be Supported

This Proposal Should Have Multiple Vote Options

Integer Overflow To Float Behavior

Backward Incompatible Changes

Proposed PHP Version(s)

RFC Impact

To Existing Extensions

Unaffected PHP Functionality

Future Scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment