On Wed, 11 Aug 2010, Derick Rethans wrote:
On Wed, 11 Aug 2010, Stas Malyshev wrote:
So I'd propose doing the following:
1. Moving parameter typing to a feature branch (by branching current trunk and
then rolling back the typing part in the trunk).
2. Starting 5.4 alpha process after that basing on trunk.
Any objections to this?
A little bit; yes. There is indeed 0 consensus for having the strict
typehints. However, instead of removing it altogether, and instead
answering every mail in this thread :P, I wrote/am writing a patch that
removes the hard type checks. It however keeps the parsed structures and
reflection API for it. In this sense, they're actually real hints. The
patch also adds a mechanism similariy to the zend_error_cb mechanism so
that extensions could override the argument type checking. As my use
case for strict checking is development I'd be happy to just move the
hard checks into an extension. I could even offer a soft check. It also
allows some type inference which might be useful for webservice
introspecition generation. I am sure SOAP might have some benefit of
this, and I know that at least pecl/dbus does. The patch is attached,
but not ready (I haven't remove the hard checks yet because things got
busy at work).
I've spend some more time on this, and have attached a new patch that:
- Removes the strict type verification, changing it back into typehints
only.
- Keeps the current syntax so that typehints create structures in the
function entries.
- Keeps the reflection API for the syntax, so that you can query the
typehints.
- Changed the API so that the verification function can also modify the
variables.
I've also written a proof of concept extension at
svn://svn.xdebug.org/svn/php/typed and
http://svn.xdebug.org/cgi-bin/viewvc.cgi/typed/?root=php that implements
both the current strict-type verification, and a form of the "option 1"
from
http://wiki.php.net/rfc/typecheckingstrictandweak#option_1_current_type_juggeling_rules_with_e_strict_on_data_loss
(I've not done the E_STRICT warnings for all, as the RFC didn't specifiy
when data loss was considered to occur).
Here follows an example script:
<?php
function errorHandler( $errno, $string )
{
global $ok;
$ok = $errno;
return true;
}
set_error_handler( 'errorHandler' );
$twelve = 12;
settype( $twelve, 'float' );
$values = array(
true, false,
0, 1, 12, $twelve, 12.23,
'true', 'false',
'0', '1', '12', '12abc', '12.0', '12.34', 'foo',
array(1,2,3), array('345' => 12),
NULL, ''
);
$funcs = array(
'testString', 'testFloat', 'testInt', 'testNumeric',
'testScalar', 'testBool', 'testArray',
);
function testString( string $a ) { }
function testFloat( float $a ) { }
function testInt( int $a ) { }
function testBool( boolean $a ) { }
function testArray( array $a ) { }
function testNumeric( numeric $a ) { }
function testScalar( scalar $a ) { }
echo "string float int numeric scalar bool array\n";
foreach( $values as $value )
{
foreach( $funcs as $func )
{
$ok = true;
$func($value);
echo $ok === true ? "pass " : ( $ok === 0x1000 ? "fail " :
"warn " );
}
echo ' ', str_replace( "\n", '', var_export( $value, true ) );
echo "\n";
}
?>
And the output (with the three different validation/verification methods:
No validation/verification:
der...@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=0 option2.php
string float int numeric scalar bool array
pass pass pass pass pass pass fail true
pass pass pass pass pass pass fail false
pass pass pass pass pass pass fail 0
pass pass pass pass pass pass fail 1
pass pass pass pass pass pass fail 12
pass pass pass pass pass pass fail 12
pass pass pass pass pass pass fail 12.23
pass pass pass pass pass pass fail 'true'
pass pass pass pass pass pass fail 'false'
pass pass pass pass pass pass fail '0'
pass pass pass pass pass pass fail '1'
pass pass pass pass pass pass fail '12'
pass pass pass pass pass pass fail '12abc'
pass pass pass pass pass pass fail '12.0'
pass pass pass pass pass pass fail '12.34'
pass pass pass pass pass pass fail 'foo'
pass pass pass pass pass pass pass array ( 0 => 1, 1
=> 2, 2 => 3,)
pass pass pass pass pass pass pass array ( 345
=> 12,)
pass pass pass pass pass pass fail NULL
pass pass pass pass pass pass fail ''
Script type-checks:
der...@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=1 option2.php
string float int numeric scalar bool array
fail fail fail fail pass pass fail true
fail fail fail fail pass pass fail false
fail fail pass pass pass fail fail 0
fail fail pass pass pass fail fail 1
fail fail pass pass pass fail fail 12
fail pass fail pass pass fail fail 12
fail pass fail pass pass fail fail 12.23
pass fail fail fail pass fail fail 'true'
pass fail fail fail pass fail fail 'false'
pass fail fail pass pass fail fail '0'
pass fail fail pass pass fail fail '1'
pass fail fail pass pass fail fail '12'
pass fail fail fail pass fail fail '12abc'
pass fail fail pass pass fail fail '12.0'
pass fail fail pass pass fail fail '12.34'
pass fail fail fail pass fail fail 'foo'
fail fail fail fail fail fail pass array ( 0 => 1, 1
=> 2, 2 => 3,)
fail fail fail fail fail fail pass array ( 345
=> 12,)
fail fail fail fail fail fail fail NULL
pass fail fail fail pass fail fail ''
"Option 1" type-casting:
der...@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=2 option2.php
string float int numeric scalar bool array
pass pass pass fail pass pass fail true
pass pass pass fail pass pass fail false
pass pass pass pass pass pass fail 0
pass pass pass pass pass pass fail 1
pass pass pass pass pass pass fail 12
pass pass warn pass pass pass fail 12
pass pass warn pass pass pass fail 12.23
pass fail warn fail pass pass fail 'true'
pass fail warn fail pass pass fail 'false'
pass pass pass pass pass pass fail '0'
pass pass pass pass pass pass fail '1'
pass pass pass pass pass pass fail '12'
pass fail warn fail pass pass fail '12abc'
pass pass pass pass pass pass fail '12.0'
pass pass pass pass pass pass fail '12.34'
pass fail warn fail pass pass fail 'foo'
fail fail fail fail fail fail pass array ( 0 => 1, 1
=> 2, 2 => 3,)
fail fail fail fail fail fail pass array ( 345
=> 12,)
pass pass pass fail fail pass fail NULL
pass fail warn fail pass pass fail ''
I'd hope to commit this patch in the next week or so, so that we can get
started with 5.4.
regards,
Derick