Re: [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept
| From: | Daniel Macedo | Date: | Mon, 05 Mar 2012 21:42:13 +0000 |
| Subject: | Re: [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-58623@lists.php.net to get a copy of this message | ||
Sorry, I used unset in the same way type casting works, not as in
unset() ... Common gotcha: http://php.net/unset#example-4824
Here's how the manual refers to it:
http://php.net/type-juggling
(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)
I understood what you meant but I'd rather have it be more verbose but
more clear as well.
I read your idea, as "Cast to int, or accept NULL" I rather
use/read that as (int unset) rather than (int?)
> > BTW: Order would equal what is type casted OR simply accepted!
>
> Do you have any examples where this could be useful?
The same example you gave of data that comes from a database NULL
would be retained, else it's type casted to int.
// Cast to int, accept null
(int unset) 13 // 13
(int unset) '' // 0
(int unset) 0 // 0
(int unset) NULL // NULL
(int unset) '342.3Test' // 342
My view is, if you use the naming as it's currently used in type
casting, you also get additional functionality:
// Cast to string, accept bool
(string bool) 'test' // 'test'
(string bool) 123 // '123'
(string bool) TRUE // TRUE
(string bool) FALSE // FALSE
(string bool) NULL // ''
// Cast to string, accept array
(string array) 'test' // 'test'
(string array) 123 // '123'
(string array) TRUE // '1'
(string array) array(1, 2, 3) // array(3) { [0]=> int(1) [1]=>
int(2) [2]=> int(3) }
I find this way more useful/complete/readable than what you proposed.
Best regards,
~ Daniel Macedo