rfc:dbc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:dbc [2015/02/12 14:05] francoisrfc:dbc [2017/09/22 13:28] – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2015-02-09   * Date: 2015-02-09
   * Author: François Laupretre <francois@php.net>   * Author: François Laupretre <francois@php.net>
-  * Status: Draft+  * Status: Discussion
   * First Published at: http://wiki.php.net/rfc/dbc   * First Published at: http://wiki.php.net/rfc/dbc
 +
 +  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 "Design by Contract Introduction" RFC
 +
 +  * https://wiki.php.net/rfc/introduce_design_by_contract
 +
 +There is alternative implementation proposal by "Definition"
 +
 +  * https://wiki.php.net/rfc/dbc2
 +
  
 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 62: Line 77:
 * This function computes the area of a triangle using Heron's formula. * This function computes the area of a triangle using Heron's formula.
 * *
-* @param float $a Length of 1st side+* @param number $a Length of 1st side
 * @requires ($a >= 0) * @requires ($a >= 0)
-* @param float $b Length of 2nd side+* @param number $b Length of 2nd side
 * @requires ($b >= 0) * @requires ($b >= 0)
-* @param float $c Length of 3rd side+* @param number $c Length of 3rd side
 * @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 float The triangle area+* @return number The triangle area
 * @ensures ($> >= 0) * @ensures ($> >= 0)
 */ */
Line 94: Line 109:
  
 $area=triangleArea('foo',2,3); $area=triangleArea('foo',2,3);
- -> PHP Fatal error: triangleArea: DbC input type mismatch - $a should match 'float' (string(3) "foo") in xxx on line nn+ -> PHP Fatal error: triangleArea: DbC input type mismatch - $a should match 'number' (string(3) "foo") in xxx on line nn
  
 $area=triangleArea(10,2,3); $area=triangleArea(10,2,3);
Line 143: Line 158:
 /*-- Properties */ /*-- Properties */
  
-/** @var float Side lengths */+/** @var number Side lengths */
  
 private $a,$b,$c; private $a,$b,$c;
Line 149: Line 164:
 //--------- //---------
 /** /**
-* @param float $a Length of 1st side +* @param number $a Length of 1st side 
-* @param float $b Length of 2nd side +* @param number $b Length of 2nd side 
-* @param float $c Length of 3rd side+* @param number $c Length of 3rd side
 * *
 * 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's formula. * This function computes the area of a triangle using Heron's formula.
 * *
-* @return float The triangle area+* @return number The triangle area
 * @ensures ($> >= 0) * @ensures ($> >= 0)
 */ */
Line 191: Line 206:
  
 $t=new triangle('foo',2,3); $t=new triangle('foo',2,3);
- -> PHP Fatal error: triangle::__construct: DbC input type mismatch - $a should match 'float' (string(3) "foo") in xxx on line nn+ -> PHP Fatal error: triangle::__construct: DbC input type mismatch - $a should match 'number' (string(3) "foo") in xxx on line nn
  
 $area=triangleArea(10,2,3); $area=triangleArea(10,2,3);
Line 268: Line 283:
 type = "integer" type = "integer"
  | "integer!"  | "integer!"
- | "float"+ | "number"
  | "float!"  | "float!"
  | "string"  | "string"
Line 308: Line 323:
 ^ integer    |  No      Yes    |  (2)      |  No        |  No      |  No        (3)      |  No         | ^ integer    |  No      Yes    |  (2)      |  No        |  No      |  No        (3)      |  No         |
 ^ integer!    No      Yes    |  No        No        |  No      |  No        No        No         | ^ integer!    No      Yes    |  No        No        |  No      |  No        No        No         |
-float      |  No      Yes    |  Yes      |  No        |  No      |  No        (4)      |  No         |+number     |  No      Yes    |  Yes      |  No        |  No      |  No        (4)      |  No         |
 ^ float!      No      No      Yes      |  No        |  No      |  No        No        No         | ^ float!      No      No      Yes      |  No        |  No      |  No        No        No         |
 ^ string      No      Yes    |  Yes      |  No        |  No      |  (6)      |  Yes      |  No         | ^ string      No      Yes    |  Yes      |  No        |  No      |  (6)      |  Yes      |  No         |
 ^ string!    |  No      No      No        No        |  No      |  (6)      |  Yes      |  No         | ^ string!    |  No      No      No        No        |  No      |  (6)      |  Yes      |  No         |
 ^ array      |  No      No      No        No        |  Yes      No        No        No         | ^ array      |  No      No      No        No        |  Yes      No        No        No         |
-^ callable    No      No      No        No        |  (5)     |  No       |  (5)      |  No         |+^ callable    No      No      No        No        |  (5)     |  (5)      |  (5)      |  No         |
 ^ object      No      No      No        No        |  No      |  Yes      |  No        No         | ^ object      No      No      No        No        |  No      |  Yes      |  No        No         |
 ^ resource    No      No      No        No        |  No      |  No        No        Yes        | ^ resource    No      No      No        No        |  No      |  No        No        Yes        |
Line 349: Line 364:
 Synonyms: 'int!' Synonyms: 'int!'
  
-== float ==+== number ==
  
 Any value that returns true through is_numeric(). Any value that returns true through is_numeric().
Line 355: Line 370:
 Equivalent to 'is_numeric($arg)'. Equivalent to 'is_numeric($arg)'.
  
-Synonyms: 'numeric', 'number'+Synonyms: 'numeric', 'float'
  
 == float! == == float! ==
Line 389: Line 404:
 == callable == == callable ==
  
-A string or array returning true through 'is_callable($arg,true)'.+A string, object or array returning true through 'is_callable($arg,true)'.
  
 Please consult the [[http://php.net/manual/en/function.is-callable.php|is_callable() documentation]] for more details. Please consult the [[http://php.net/manual/en/function.is-callable.php|is_callable() documentation]] for more details.
Line 739: Line 754:
  
 [[https://wiki.php.net/rfc/dbc2|Alternative RFC]] [[https://wiki.php.net/rfc/dbc2|Alternative RFC]]
- 
rfc/dbc.txt · Last modified: 2018/03/01 23:19 by carusogabriel