Hi!
I am a userland developer, so please take my advice with caution.  I am not
aware of the consequences of any of the implementations from an internals
point of view.  However, since the main question here seems to be strict
against weak typing, or casting, I feel I should voice my opinion.

>From a user perspective, I feel that PHP does a very good job at casting
variables if needed.  One should take care not to do anything stupid but
heh, that's programming...  I therefore think using rules similar to the
casting rules is a good suggestion.  It is easy to understand, easy to use
as a library developer and mostly, it is very easy to use for library users
(no change is needed to their code).

A function might look like this in PHP <= 5.3.0:
function foo ($x) {
   return $x + 5; // hoping the user sends a numeric value, else the results
will be silently wrong
}

It could be used like this:
foo(5); // ok
foo('hello'); // wrong but no error
foo(fopen('hello')); // wrong but no error
foo(new CustomClass()); // wrong but no error

Or it could be written to check the type of the parameter:
function foo ($x) {
   $y = (float) $x; // explicit cast
   return $y + 5; // we know what type we have so we know the operation is
safe, we don't need any errors to be shown
}

The problem, as everyone is well aware, is that we need either to change the
value of $x (which I believe is bad practice) or to have a copy of $x cast
into $y, which of course means performance decrease and increased memory
consumption.


So, we all agree PHP should provide means to help developers simplify the
validation chain: type hinting (ie. telling the developer the parameters
should be converted before calling the function) or casting (ie. following
specific rules to cast to the right type).


With strong type hinting, the function would become:
function foo (float $x) { // if the user does not send a float value, the
function fails
   echo $x + 5;
}

It could be used like this:
foo(5); // some kind of explicit error
foo((float) 5); // ok
foo(5.0); // ok
foo('hello'); // some kind of explicit error
foo((float) 'hello'); // wrong but that much is plain
foo(fopen('hello')); // some kind of explicit error
foo((float) fopen('hello')); // wrong but that much is plain
foo(new CustomClass()); // some kind of explicit error
foo((float) new CustomClass()); // wrong but that much is plain


We all agree, I hope, that this makes userland code very ugly, not to
mention very repetitive. Each time I call foo(), PHP would make me write a
manual cast on the parameters every time I call the function.  I am quite
sure it helps to make the code a lot safer, but this is not at all what PHP
developers are used to do. And it would *not* help PHP's fame.  To a user,
that would make PHP close to C or C++, but without the good performance.  I
can see very little gain here.

Here is another example:
foo($var);

If I am not sure what type $var is at the time I call foo(), because this is
PHP and I have never had to worry about it before, strong type hinting would
have me do this:
foo((float) $var);

If I did that, my users would simply drop my libraries because they would
not appear to be written in PHP.  Not the PHP they like, anyway.  After all,
why would my libraries refuse an integer value when they expect a float?
 And of course, PHP already has internal rules to convert several types to
others.  Why not use that?

So, let's take a user point of view.  As such, I defnitely would like to be
able to call my function foo() without any explicit casting every time I
need the function, under the threat that I would have errors when PHP should
already be able to cast the parameters.

foo(5); // ok
foo('hello'); // ok, empty
foo(fopen('hello')); // ok, could be empty or cast to: "0" for error, "1"
otherwise (like booleans in other languages)
foo(new CustomClass()); // ok, empty or maybe a handler can be set to
automatically cast in this case?

Of course, these are set values.  Day to day programming does not always
involve hard-coded values like this but rather user-supplied values, ie.
mostly text values.  I my experience, most of the time text values are easy
to cast to other types.  I think this is why type hinting/casting could be
of great help.

Maybe a nice addition would be a specific error level so that I could see
where in my code these silent castings should take place.  Or where they do
take place.  Or neither.

foo($bar); // throws an error only if error_level is set to E_HINT


Bottom line: As a user of libraries, I see no good in strong hint typing.
 However, as a library developer I see uses for it.


Best regards,

--
Guillaume Rossolini

Reply via email to