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 revisionBoth sides next revision
rfc:unset_bool [2013/03/06 22:34] – + microbenchmark bwoebirfc:unset_bool [2013/03/07 12:08] – + use case bwoebi
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.53+  * Version: 0.6
   * Date: 2013-03-06    * Date: 2013-03-06 
   * Author: Bob Weinand, bobwei9@hotmail.com   * Author: Bob Weinand, bobwei9@hotmail.com
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 121: Line 158:
   * Version 0.52: echo may be also considered as a function...   * Version 0.52: echo may be also considered as a function...
   * Version 0.53: Added microbenchmark   * Version 0.53: Added microbenchmark
 +  * Version 0.54: Added example real world use case
rfc/unset_bool.txt · Last modified: 2018/06/18 10:14 by cmb