I like and will use it a lot... sometimes you simple cannot trust on php
dynamic type convertion, please see the following example
<?php
function check_user($user_id) {
if ($user_id == 0) {
// root user
} else if ($user_id > 0 && $user_id < 1000) {
// special admin users
} else {
// standard users
}
}
?>
Now if someone thinks $user_id is the user login name and call
check_user('<login_name>');
'<login_name>' will be converted to 0 (zero) as per php automatic string
to numeric convertion and this user will be handled as being the root
user. In this case strict comparison "===" is not apropriate, and I'm
forced to verify the parameter against is_numeric.
I much simpler approach would be...
<?php
function check_user(int $user_id) {...}
Also I think this is a basic prerequisite to allowing function overload
on php OO side in the future ;)
Thanks,
Jaris.
Stanislav Malyshev wrote:
Hi!
So:
function foo($var) { if(!is_int($var)) { throw new exception('not
int'); }}
What's the use of such code? If $var is '1' and not 1, what's the use
of throwing an exception and having to handle it later (basically by
failing the task, since you don't know how to do foo() now) - instead
of just doing with that 1 what was intended for? There's no any
difference between 1 and '1' that can be important to anybody. Only
difference is the way it is represented in underlying bits in zvals,
about which nobody should ever care. That's like making function that
would accept only arguments that has 3'rd bit of pointer set to 1 and
5th bit set to 0, and reject all others. No sane application should
ever behave this way. Writing such function is just plain wrong, it
replaces the substance of programming with nitpicking over the details
that are not important. Whole phenomenon of dynamic languages has
grown on the principle of liberating people from caring for bits and
concentrate on substance, and now you try to drag the bits back in.
which is called like this in both cases, maybe with a try catch etc etc:
foo((int) $baz['bar']);
So every time you call foo you need try/catch? And that's supposed to
be _good_?
--
*Jarismar Chaves da Silva, M.Sc.*