rfc:introduce-type-affinity

PHP RFC: Introduce Type Affinity

Introduction

PHP is weakly typed to make it work with Web smoothly. However, precise data type usage is more an more important in these days. Weakly typed does not mean typeless. SQLite2 was typeless, but SQLite3 became type aware by “Type Affinity” to achieve better performance.

https://www.sqlite.org/datatype3.html

SQLite3 stores data as native data type if input for the field is appropriate for field data type.

PHP may have type affinity for both better performance and code.

Since inputs are converted to native type.

  • PHP does not have to convert ZVAL again and again. (e.g. “int” input is almost always treated as “int”)
  • It works well with coercive type or strict type if PHP has it. (Not work well with currently proposed “strict_types”, though)
  • User can omit simple data type validation and concentrate what data should like.

Proposal

Introduce SQLite3 like type affinity for $_GET/$_POST/$_COOKIE/$_FILES/$_ENV/$_SERVER:

There are few possible ways.

  • Detect possible data type without definition/hint
  • Have INI option for type affinity conversion (When “On”, affinity conversion is done always for $_GET/$_POST/etc)
  • Data is stored with native data type
  • Conversion will never raise error
  • Provide data type definition/hint prior to the conversion
  • Apply type affinity function and convert (User must write code unlike previous one)
  • If there is conversion error, it may raise error

Introduce type affinity conversion function:

  • void type_affinitize(mixed &$input [, array $definition ]);
  • void type_affinitize_input(string $input=“GPC” [, array $definition ]);
    “GPC” is for $_GET/$_POST/$_COOKIE

Type affinity may be applied to any inputs including databases/$argv/etc.

Please suggest other ways/ideas. Thank you.

Backward Incompatible Changes

Inputs (e.g. $_GET/etc) has string data typed always currently. Type affinity introduce native type for inputs.

Proposed PHP Version(s)

PHP 7.x

RFC Impact

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

New Constants

None.

php.ini Defaults

If there are any php.ini settings then list:

  • hardcoded default values
  • php.ini-development values
  • php.ini-production values

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

Future Scope

Type affinity can increase usefulness of strict data typing.

Proposed Voting Choices

Include these so readers know where you are heading and can discuss the proposed voting options.

State whether this project requires a 2/3 or 50%+1 majority (see voting)

Patches and Tests

TBD

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Links to external references, discussions or RFCs

  • This RFC related to “type hint” RFCs.

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/introduce-type-affinity.txt · Last modified: 2017/09/22 13:28 (external edit)