[trim]

> 2. "Strict type hinting would eliminate PHP's flexibility and take away its
> unique simplicity."
>
> I respectfully disagree.  Again, let me remind you that we are *not*
> talking
> about *converting *PHP to strict type hinting.  Instead, we're merely
> talking about allowing PHP developers to *choose* whether or not to make a
> given function use dynamic or strict type hinting.  The default behavior
> will remain dynamic, just as it is now.  But there are situations where
> strict type hinting, even in a PHP script, would make more sense.  There
> are many PHP developers, myself among them, who see considerable benefit in
> being able to make a function more condensed and streamlined without having
> to waste so much time on sanity checks that could instead be handled at a
> lower level in the core.
>
>
So this is the argument that those who object to strict type hinting don't
agree with. Take the following:

function strictTypes(/*int*/ $var)
{
    //this is what the engine does if we have strict type checking
    if(!is_int($var)) trigger_error();
}

function weakTypes(/*int*/ $var)
{
    //this is what the engine does if we have weak type hinting, or
something similar.
    if(!is_numeric($var) || (int)$var != $var) trigger_error();
    else $var = (int)$var;
}

function dynamicTypes($var)
{
    strictTypes((int) $var);
    //if $var is not an int, we just made it 0, and hid the type error.
    //to avoid this mistake we have to do:
    strictTypes(is_int($var) ? $var : ((is_numeric($var) && (int)$var ==
$var) ? (int)$var : trigger_error());
    //or something like it.
    weakTypes($var);
    //we'll get an error if $var can't be converted to an int without data
loss.
}

By calling the strictTypes() function, the dynamicTypes() function inherits
the problem of validating the type of $var. Well, if I'm writing the
dynamicTypes function, I don't want that work, so I push it up the chain,
and change my dynamicTypes function to statically typed. If you're into
static types, then you say, that's great, someone should make sure that
$var has the right type when they got it from the user. But if you're not
into static types, you were just forced to do type checking, either in your
code, or passing it up the call chain for someone else to do the type
checking. That's what is meant when we say dynamic typing can't really
coexist with strict typing. For those into dynamic types, weak type hinting
is much more palatable, because it doesn't require callers to adopt the
same philosophy.

If you want type hinting, you'll have to specify which kind you want,
strict or weak. If it's strict type hinting, you'll need to convince even
those who think dynamic typing is a guiding principle of PHP that it can be
done without forcing strict typing up the call chain. Weak type hinting is
a softer sell, but requires a lot of thought(much of which has been done,
if you look in previous discussions) , about how and when to convert values.

Reply via email to