rfc:dbc
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:dbc [2015/02/12 13:44] – francois | rfc:dbc [2018/03/01 23:19] (current) – Typo "Under Discussion" carusogabriel | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2015-02-09 | * Date: 2015-02-09 | ||
* Author: François Laupretre < | * Author: François Laupretre < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | |||
+ | This RFC is waiting for the decisions that will be made about scalar | ||
+ | type hinting. The reason is that the design and syntax | ||
+ | decisions that will be made about scalar type hinting heavily impact the | ||
+ | contents of this RFC. Proposal is subject to be changed according scalar type | ||
+ | hinting implementation. | ||
===== Preamble ===== | ===== Preamble ===== | ||
+ | |||
+ | This RFC is part of " | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | There is alternative implementation proposal by " | ||
+ | |||
+ | * https:// | ||
+ | |||
The original idea of introducing DbC in PHP comes from Yasuo Ohgaki | The original idea of introducing DbC in PHP comes from Yasuo Ohgaki | ||
Line 22: | Line 37: | ||
For more than 10 years (since PHP 5 was released), the PHP core community has | For more than 10 years (since PHP 5 was released), the PHP core community has | ||
seen a lot of discussions about strict vs loose typing, type hinting and | seen a lot of discussions about strict vs loose typing, type hinting and | ||
- | related features. Through these discussions, | + | related features. Through these discussions, |
them as early as possible. Strictifying types is an approach but, unfortunately, | them as early as possible. Strictifying types is an approach but, unfortunately, | ||
so well with PHP as a loose-typed language. | so well with PHP as a loose-typed language. | ||
Line 62: | Line 77: | ||
* This function computes the area of a triangle using Heron' | * This function computes the area of a triangle using Heron' | ||
* | * | ||
- | * @param | + | * @param |
* @requires ($a >= 0) | * @requires ($a >= 0) | ||
- | * @param | + | * @param |
* @requires ($b >= 0) | * @requires ($b >= 0) | ||
- | * @param | + | * @param |
* @requires ($c >= 0) | * @requires ($c >= 0) | ||
* @requires ($a <= ($b+$c)) | * @requires ($a <= ($b+$c)) | ||
Line 72: | Line 87: | ||
* @requires ($c <= ($a+$b)) | * @requires ($c <= ($a+$b)) | ||
* | * | ||
- | * @return | + | * @return |
* @ensures ($> >= 0) | * @ensures ($> >= 0) | ||
*/ | */ | ||
Line 94: | Line 109: | ||
$area=triangleArea(' | $area=triangleArea(' | ||
- | -> PHP Fatal error: triangleArea: | + | -> PHP Fatal error: triangleArea: |
$area=triangleArea(10, | $area=triangleArea(10, | ||
Line 143: | Line 158: | ||
/*-- Properties */ | /*-- Properties */ | ||
- | /** @var float $a, | + | /** @var number |
private $a,$b,$c; | private $a,$b,$c; | ||
Line 149: | Line 164: | ||
//--------- | //--------- | ||
/** | /** | ||
- | * @param | + | * @param |
- | * @param | + | * @param |
- | * @param | + | * @param |
* | * | ||
* No need to repeat constraints on values as they are checked by class invariants. | * No need to repeat constraints on values as they are checked by class invariants. | ||
Line 169: | Line 184: | ||
* This function computes the area of a triangle using Heron' | * This function computes the area of a triangle using Heron' | ||
* | * | ||
- | * @return | + | * @return |
* @ensures ($> >= 0) | * @ensures ($> >= 0) | ||
*/ | */ | ||
Line 191: | Line 206: | ||
$t=new triangle(' | $t=new triangle(' | ||
- | -> PHP Fatal error: triangle:: | + | -> PHP Fatal error: triangle:: |
$area=triangleArea(10, | $area=triangleArea(10, | ||
Line 215: | Line 230: | ||
* it allows to keep the source code executable on previous PHP interpreters. | * it allows to keep the source code executable on previous PHP interpreters. | ||
* Phpdoc comments, while not perfect, have always played the role of annotations in PHP. ' | * Phpdoc comments, while not perfect, have always played the role of annotations in PHP. ' | ||
- | * DbC can use part of already-written phpdoc | + | * DbC can use a great part of the already-written phpdoc |
Note: Some people on the mailing list are religiously opposed to including information | Note: Some people on the mailing list are religiously opposed to including information | ||
Line 240: | Line 255: | ||
seen as built-in conditions. | seen as built-in conditions. | ||
- | Here are the main benefits of defining | + | Here are the main benefits of defining |
* PHP is_xxx() functions are not as intuitive as they may seem, as they are based on zval types (an equivalent of strict type checks). They are not appropriate for people who just want to accept a limited set of type juggling (accepting a numeric string from a DB, for instance). Unfortunately, | * PHP is_xxx() functions are not as intuitive as they may seem, as they are based on zval types (an equivalent of strict type checks). They are not appropriate for people who just want to accept a limited set of type juggling (accepting a numeric string from a DB, for instance). Unfortunately, | ||
* As it was already said, tons of source code already contains argument return/ | * As it was already said, tons of source code already contains argument return/ | ||
* Readability is a key point too: just compare a type like ' | * Readability is a key point too: just compare a type like ' | ||
+ | * DbC types allow static analysis, which is practically impossible with conditions. | ||
+ | * A lot of other analyzis/ | ||
DbC types are used to check : | DbC types are used to check : | ||
Line 265: | Line 282: | ||
type = " | type = " | ||
- | | " | + | | " |
+ | | " | ||
+ | | "float!" | ||
| " | | " | ||
+ | | " | ||
| array-type | | array-type | ||
| " | | " | ||
Line 275: | Line 295: | ||
| " | | " | ||
| " | | " | ||
+ | | " | ||
array-type = " | array-type = " | ||
Line 288: | Line 309: | ||
=== DbC types vs zval types === | === DbC types vs zval types === | ||
- | DbC types have specific rules to match PHP zvals. These rules are less permissive than | + | DbC types follow |
PHP API type juggling and previously-proposed scalar ' | PHP API type juggling and previously-proposed scalar ' | ||
these types try to be a more intuitive compromise between both. | these types try to be a more intuitive compromise between both. | ||
Line 302: | Line 323: | ||
^ integer | ^ integer | ||
^ integer! | ^ integer! | ||
- | ^ float | + | ^ number |
^ float! | ^ float! | ||
^ string | ^ string | ||
^ string! | ^ string! | ||
^ array | No | ^ array | No | ||
- | ^ callable | + | ^ callable |
^ object | ^ object | ||
^ resource | ^ resource | ||
Line 323: | Line 344: | ||
* (6) only if class defines a %%__%%toString() method\\ | * (6) only if class defines a %%__%%toString() method\\ | ||
* (7) O is false, 1 is true. Other values don't match (to be discussed) | * (7) O is false, 1 is true. Other values don't match (to be discussed) | ||
- | |||
- | You may note that this is much more restrictive that PHP native type juggling. | ||
=== DbC types === | === DbC types === | ||
Line 345: | Line 364: | ||
Synonyms: ' | Synonyms: ' | ||
- | == float == | + | == number |
Any value that returns true through is_numeric(). | Any value that returns true through is_numeric(). | ||
Line 351: | Line 370: | ||
Equivalent to ' | Equivalent to ' | ||
- | Synonyms: ' | + | Synonyms: ' |
== float! == | == float! == | ||
Line 385: | Line 404: | ||
== callable == | == callable == | ||
- | A string or array returning true through ' | + | A string, object |
Please consult the [[http:// | Please consult the [[http:// | ||
Line 678: | Line 697: | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | PHP 7. Backporting to PHP 5 is possible if implemented as a separate extension. | + | As the plan is to implement this in a separate extension, it should be availbale for PHP 5 ans PHP 7. |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 735: | Line 754: | ||
[[https:// | [[https:// | ||
- |
rfc/dbc.1423748680.txt.gz · Last modified: 2017/09/22 13:28 (external edit)