rfc:unset_bool

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:unset_bool [2013/03/06 21:27] – [Execution example] Multiple unset's in one forgotten bwoebirfc:unset_bool [2018/06/18 10:14] (current) – This RFC appears to be inactive cmb
Line 1: Line 1:
 ====== PHP RFC: unset(): return bool if the variable has existed ====== ====== PHP RFC: unset(): return bool if the variable has existed ======
-  * Version: 0.5+  * Version: 0.61
   * Date: 2013-03-06    * Date: 2013-03-06 
   * Author: Bob Weinand, bobwei9@hotmail.com   * Author: Bob Weinand, bobwei9@hotmail.com
-  * Status: Under Discussion+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/unset_bool   * First Published at: http://wiki.php.net/rfc/unset_bool
   * Implementation: https://github.com/bwoebi/php-src/commit/787d71eed0c5e0140048b8fbacf799029b322661 (tests may follow later)   * Implementation: https://github.com/bwoebi/php-src/commit/787d71eed0c5e0140048b8fbacf799029b322661 (tests may follow later)
Line 11: Line 11:
 The purpose of this RFC is to make unset return something meaningful. People also shouldn't have to wonder why they can't use unset if it isn't as a standalone expression. The purpose of this RFC is to make unset return something meaningful. People also shouldn't have to wonder why they can't use unset if it isn't as a standalone expression.
  
-This removes also an inconsistency: the function's return value is //void//, states the docs. It is until now the only function which has no return value.+This removes also an inconsistency: the function's return value is //void//, states the docs. It is until now the only function (language construct, function-like) (apart from echo, which already has an alternative: print) which has no return value.
  
 ===== Proposal ===== ===== Proposal =====
Line 17: Line 17:
 Change unset()'s behaviour to: Change unset()'s behaviour to:
   * return true if deletion was successful   * return true if deletion was successful
-  * return false if deletion failed+  * return false if deletion failed (e.g. there was nothing to delete or the deleting function has failed)
  
 The feature could be useful in if's like: The feature could be useful in if's like:
Line 75: Line 75:
 var_dump(unset($class->quarta)); // bool(false) (a function execution without return returns NULL, which is, casted to boolean, false) var_dump(unset($class->quarta)); // bool(false) (a function execution without return returns NULL, which is, casted to boolean, false)
 </code> </code>
 +
 +===== Use case =====
 +<code php>
 +class User {
 + private $data = [];
 +
 + // here a __set and a __get function for $data
 +
 + public function __unset ($key) {
 + switch ($key) {
 + case "id":
 + return false;
 + default:
 + return unset($this->data[$key]);
 + }
 +}
 +
 +class UserManager {
 + private $Users = [];
 +
 + public function __construct($data) {
 + // some code to initialize the $this->Users array
 + }
 +
 + public function reset($userId, $key) {
 + if (!unset($this->Users[$userId]->$key)) {
 + throw new Exception("You can't reset the `$key` of '{$this->Users[$userId]->name}'");
 + }
 + }
 +}
 +
 +$manager = new UserManager($data);
 +
 +$manager->reset($id, "id"); // now you have a meaningful debugging output through classes as current behaviour would be to do nothing - silently.
 +</code>
 +
 +This genre of code could be also in Frameworks to indicate to a coder that the variable can be accessed, but not deleted.
  
 ===== Implementation Details === ===== Implementation Details ===
Line 93: Line 130:
  
 Should be implemented in the next 5.x (e.g. 5.5 if it's delayed due to ZO+ or 5.6). Should be implemented in the next 5.x (e.g. 5.5 if it's delayed due to ZO+ or 5.6).
 +
 +===== Microbenchmark =====
 +
 +<code>
 +time ./sapi/cli/php -r 'while($i++ < 1e7) { $a = true; unset($a); }'
 +
 +Unpatched: average of 5x executed:
 +real 0m4.935s
 +user 0m4.925s
 +sys 0m0.008s
 +
 +Patched: average of 5x executed:
 +real 0m4.945s
 +user 0m4.938s
 +sys 0m0.005s
 +</code>
 +This is an increase of 0.15%. This is 1 nanosecond per execution more than previously.
  
 ===== References ===== ===== References =====
Line 101: Line 155:
  
   * Version 0.5: Initial RFC   * Version 0.5: Initial RFC
 +  * Version 0.51: Added example for multiple unset()'s in one
 +  * Version 0.52: echo may be also considered as a function...
 +  * Version 0.53: Added microbenchmark
 +  * Version 0.6: Added example real world use case
 +  * Version 0.61: Little clarification about failures
rfc/unset_bool.txt · Last modified: 2018/06/18 10:14 by cmb