On Sat, Jan 05, 2008 at 10:13:29PM +0100, Stefan Esser wrote:
> 
> >> opinion) reason why type hinting should NOT be introduced. BTW accepting
> >> the string '1' where an (int) type hint is placed would be the next
> >> stupid design decision.
> >>     
> >
> > Why 
> Because type hinting is supposed to limit what kind of variable type is
> allowed for a parameter. When you magically convert you kill the whole

Ahah - I think that the above identifies one of two different views of
what is meant by type hinting:

1) type hinting means that the internal php type of a value[**]

2) type hinting means that the value could be exactly interpretted as a certain 
type.


With (1):

1 and '1' are of different types (int and string)

with (2):

1 and '1' are the same - could both be int or string (or even float)

Interpretation (2) is much more in tune with the type juggling that
PHP does and makes it a good language for dealing with the web.

I agree that type hinting style (1) has all sorts of problems.
Type hinting (2) would be useful and is what PHP needs.




[**] we are talking about values and not variables here - subtle/slight
but important difference.

> idea of type hints and replace it with some magical function parameter
> auto type conversion, which would be another hard to understand magic
> feature of PHP.

Once you have assertained that a parameter value is of a certain type, you might
as well convert it to the certain type (but keeping the value unchanged) since
the function is likely to use the parameter as that type. This will result
in faster code[%%]

> And the next problem would be what should happen in case of a magical
> conversion when the function wants an int in a parameter that is a
> reference. Suddenly calling a function does magically change variable
> types outside of the function => NIGHTMARE.

This is something that I have identified as a problem in private email
with Sam Barrow.

However: if we have *value* preserving type hinting this should not be
a problem. If the *value* cannot be 100% interpretted as something of
the type hint - then we have an error -- which would kill the script anyway.

[%%]
This shows that using the correct type can make your program faster.

Run the following script:
<?php

echo "Starting\n";

$myTime = microtime(true);

$top = '10000000';
$t = 0;

$myTime = microtime(true);
for($i = 0; $i < $top; $i++)
    $t += $i;
$myTime = microtime(true) - $myTime;
echo "top as string $myTime\n";

$top = 10000000;
$t = 0;

$myTime = microtime(true);
for($i = 0; $i < $top; $i++)
    $t += $i;
$myTime = microtime(true) - $myTime;
echo "top as number $myTime\n";
?>

The output that I get is:
Starting
top as string 6.147411108017
top as number 3.7645990848541

There is thus considerable advantage on doing the type juggling as part of the
type hinting on function call.




-- 
Alain Williams
Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: 
http://www.phcomp.co.uk/contact.php
Chairman of UKUUG: http://www.ukuug.org/
#include <std_disclaimer.h>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to