rfc:objects-can-be-falsifiable

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
rfc:objects-can-be-falsifiable [2020/07/14 23:51] – change status joshbrucerfc:objects-can-be-falsifiable [2022/11/04 23:14] (current) – Add static analysis section joshbruce
Line 1: Line 1:
 ====== PHP RFC: Objects can be declared falsifiable ====== ====== PHP RFC: Objects can be declared falsifiable ======
-  * Version: 0.9 +  * Version: 0.92 
-  * Date: 2020-07-14 +  * Date: 2020-07-16 
-  * Author: Josh Bruce, josh@joshbruce.dev +  * Author: Josh Bruce, josh@joshbruce.com 
-  * Implementer: seeking (or someone willing to answer specific questions when I have them)+  * Implementer: seeking (or someone willing to answer specific questions when I have them) - https://github.com/joshbruce/php-src/pull/2
   * Status: Under discussion   * Status: Under discussion
   * First Published at: http://wiki.php.net/rfc/objects-can-be-falsifiable   * First Published at: http://wiki.php.net/rfc/objects-can-be-falsifiable
Line 9: Line 9:
 ===== Introduction ===== ===== Introduction =====
  
-This RFC introduces a new interface <php>Falsifiable</php> and magic method <php>__toBool()</php> allowing custom objects (types) to define and declare themselves <php>true</php> or <php>false</php>. Given PHP 8 interprets a valid instance as only <php>true</php>, this would give developers the opportunity to work with an instance of a known object (type) and glean its truthiness from the instance itself, not <php>null</php> or <php>empty()</php> checks. (Or <php>strlen()</php> and other operator-required checks.)+This RFC introduces a new interface <php>Falsifiable</php> and magic method <php>__toBool()</php> allowing custom objects (types) to define and declare themselves <php>true</php> or <php>false</php>. Given PHP 8 interprets a valid instance as only <php>true</php>, this would give developers (the instance itself, and other objects) the opportunity to work with an instance of a known object (type) and glean its truthiness from the instance itself, not <php>null</php> or <php>empty()</php> checks. (Or <php>strlen()</php> and other operator-required checks.)
  
 This RFC has 3 goals: This RFC has 3 goals:
Line 145: Line 145:
 // ]; // ];
 </code> </code>
 +
 +===== Notes for static analysis ====
 +
 +Validate that the implementation can have both a true and false return, not just one or the other.
 +
 +===== Type juggling tables ====
 +
 +With no modifications or interventions by the developer and all types are empty (or false in the case of boolean):
 +
 +^Cast to         ^Type: ^^^^^^^^
 +|                ^null (unset)   ^custom type (empty) ^object    ^array     ^float                ^integer              ^string                ^bool(ean)    |
 +|unset (nullify) |null           |null                |null      |null      |null                 |null                 |null                  |null         |
 +|custom type     |error          |error               |error     |error     |error                |error                |error                 |error        |
 +|object          |object (empty) |no change           |no change |object    |object (scalar of 0) |object (scalar of 0) |object (scalar of "") |object (scalar of false) |
 +|array           |[]             |[]                  |[]        |no change |[0]                  |[0]                  |[""                 |[false]      |
 +|float           |0              |error               |error     |0         |no change            |0                    |0                     |0            |
 +|integer         |0              |error               |error     |0         |0                    |no change            |0                     |0            |
 +|string          |""             |error               |string    |error     |"0"                  |"0"                  |no change             |""           |
 +|boolean         |false          |true                |true      |false     |false                |false                |false                 |no change    |
  
 ===== Truthiness tables ===== ===== Truthiness tables =====
  
 Scalar types and their relationship to <php>false</php>: Scalar types and their relationship to <php>false</php>:
 +
  
  
Line 200: Line 220:
 ===== Open Issues ===== ===== Open Issues =====
  
 +==== November 3, 2022 ====
 +
 +  * How would this impact <php>callable</php>? Would a class using <php>__invoke()</php> default to true? Can the same class implement Falsifiable and return false?
 +
 +
 +==== July 16, 2020 ====
 +
 +  * Default value for parameters with a class type can only be NULL
 +
 +   
 +==== July 15, 2020 ====
 +
 +  * How type safe is this really? (desire is to increase type safety - partially by being able to return a single type from a method that resolves to false)
 +  * RESOLVED (see static analysis section): Impact to static analysis. Multiple static analyzers for PHP exist: Phan (Rasmus and Morrison), PHPStan (Mirtes), Psalm (Vimeo), and a general list - https://github.com/exakat/php-static-analysis-tools
 +  * Interaction with equality operators.
 +  * Language around <php>bool|Falsifiable</php> implementation and need.
 +  * What version of PHP switched to only allowing <php>__construct()</php>
 +
 +==== < July 15, 2020 ====
 +
 +  * Presumes impact similar to <php>__toString()</php> and <php>Stringable</php>. RFC for <php>Stringable</php> listed concerns related to <php>__toString()</php> already being a method. Would look at the implementation as it should be similar, level of knowledge to implement is not there yet.
   * As of this writing I do not have the knowledge, practice, and practical understanding of implementing within PHP internals to implement this myself. If you're interested in (helping) implement this concept, please do reach out (help may be in the form guidance and instruction or full implementation, up to you).   * As of this writing I do not have the knowledge, practice, and practical understanding of implementing within PHP internals to implement this myself. If you're interested in (helping) implement this concept, please do reach out (help may be in the form guidance and instruction or full implementation, up to you).
-  * Would presume impact similar to <php>__toString()</php> and <php>Stringable</php>. RFC for <php>Stringable</php> listed concerns related to <php>__toString()</php> already being a method. Would look at the implementation as it should be similar, level of knowledge to implement is not there yet.+
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
  
-  * <php>null</php> behavior remains unchanged+  * <php>null</php> behavior remains unchanged
 +  * Object can define other methods that return <php>bool</php> including one named <php>toBool()</php>.
  
 ===== Future Scope ===== ===== Future Scope =====
  
-None.+Leaves open, and does not directly pursue, the possibility of a future emptiness check or return. If passed to <php>empty()</php> a false instance would be considered empty; however, there may come a future in PHP wherein developers could specify the "emptiness" of an instance.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 224: Line 266:
  
 ===== Implementation ===== ===== Implementation =====
 +
 +Temporary PR for implementation, with status notes: https://github.com/joshbruce/php-src/pull/2
 +
 After the project is implemented, this section should contain  After the project is implemented, this section should contain 
   * the version(s) it was merged into   * the version(s) it was merged into
Line 231: Line 276:
  
 ===== References ===== ===== References =====
 +
 These RFCs are identified as similar in spirit to this [concept], possibly helped by this [concept], or this [concept] is potentially helped by the result of the linked RFC. These RFCs are identified as similar in spirit to this [concept], possibly helped by this [concept], or this [concept] is potentially helped by the result of the linked RFC.
 +
 +Originally posted on GitHub, edit history available there: [[https://bit.ly/php-0002|0.2.0]] and [[https://bit.ly/php-0001|0.1.0]]
  
 Implemented: Implemented:
  
 +  * [[https://wiki.php.net/rfc/null-false-standalone-types|PHP RFC: Allow null and false as stand-alone types]]
   * [[https://wiki.php.net/rfc/union_types_v2|PHP RFC: Union Types 2.0]] - accept multiple types, including <php>null</php>   * [[https://wiki.php.net/rfc/union_types_v2|PHP RFC: Union Types 2.0]] - accept multiple types, including <php>null</php>
   * [[https://wiki.php.net/rfc/counting_non_countables|PHP RFC: Counting of non-countable objects]] - return <php>1</php> or <php>0</php>, not <php>null</php>.   * [[https://wiki.php.net/rfc/counting_non_countables|PHP RFC: Counting of non-countable objects]] - return <php>1</php> or <php>0</php>, not <php>null</php>.
- 
-Accepted: 
- 
   * [[https://wiki.php.net/rfc/nullable_types|PHP RFC: Nullable Types]] - return <php>null</php> or one (or more) types from function or method   * [[https://wiki.php.net/rfc/nullable_types|PHP RFC: Nullable Types]] - return <php>null</php> or one (or more) types from function or method
   * [[https://wiki.php.net/rfc/stringable|PHP RFC: Add Stringable interface]] - implementation reference, automatically view object as string   * [[https://wiki.php.net/rfc/stringable|PHP RFC: Add Stringable interface]] - implementation reference, automatically view object as string
   * [[https://wiki.php.net/rfc/magic-methods-signature|PHP RFC: Ensure correct signatures of magic methods]] - implementation reference   * [[https://wiki.php.net/rfc/magic-methods-signature|PHP RFC: Ensure correct signatures of magic methods]] - implementation reference
 +  * [[https://wiki.php.net/rfc/nullsafe_operator|PHP RFC: Nullsafe operator]] - chained method calls do not halt when encountering <php>null</php>
 +
 +Accepted:
 +
 +  * [[https://wiki.php.net/rfc/true-type|PHP RFC: Add true type]] - allow specifying true or false when typesetting
  
 Under review and discussion: Under review and discussion:
  
-  * [[https://wiki.php.net/rfc/nullsafe_operator|PHP RFC: Nullsafe operator]] - chained method calls do not halt when encountering <php>null</php> 
-  * [[https://wiki.php.net/rfc/pipe-operator-v2|PHP RFC: Pipe Operator v2]] - chain using object instance using <php>__invoke()</php> 
   * [[https://wiki.php.net/rfc/to-array|PHP RFC:__toArray()]] - implementation reference and where magic method should live   * [[https://wiki.php.net/rfc/to-array|PHP RFC:__toArray()]] - implementation reference and where magic method should live
 +  * [[https://wiki.php.net/rfc/invokable|PHP RFC: Invokable]] - see also [[https://www.php.net/manual/en/function.is-callable.php|is_callable]]
  
 Declined: Declined:
  
 +  * [[https://wiki.php.net/rfc/stricter_implicit_boolean_coercions|PHP RFC: Stricter implicit boolean coercions]] - indicates desire for more control around true-false behavior
   * [[https://wiki.php.net/rfc/userspace_operator_overloading|PHP RFC: Userspace operator overloading]] - could facilitate <php>__toBool()</php> by proxy via operators   * [[https://wiki.php.net/rfc/userspace_operator_overloading|PHP RFC: Userspace operator overloading]] - could facilitate <php>__toBool()</php> by proxy via operators
 +  * [[https://wiki.php.net/rfc/pipe-operator-v2|PHP RFC: Pipe Operator v2]] - chain using object instance using <php>__invoke()</php>
 +
 +
 +Other:
 +
 +  * [[https://wiki.php.net/rfc/null_coercion_consistency|RFC describing some NULL-related issues]] and [[https://wiki.php.net/rfc/allow_null|another]]
 +  * [[https://externals.io/message/111009|Official thread]]
 +  * [[https://externals.io/message/111076|Mention of type juggling tables being added]] - started as new thread
 +  * [[https://externals.io/message/110967|Declaring emptiness]] - alt thread that was still exploring ideas for this RFC
 +  * [[https://externals.io/message/110881|Instance as boolean]] - original thread related to this RFC
 +  * [[https://externals.io/message/39323#39331|__toBoolean() brief discussion]] 11 years ago
  
 ===== Rejected Features ===== ===== Rejected Features =====
 Keep this updated with features that were discussed on the mail lists. Keep this updated with features that were discussed on the mail lists.
rfc/objects-can-be-falsifiable.1594770674.txt.gz · Last modified: 2020/07/14 23:51 by joshbruce