note 102827 added to language.oop5.typehinting
| From: | brett at nobistech dot net | Date: | Tue, 08 Mar 2011 23:49:03 +0000 |
| Subject: | note 102827 added to language.oop5.typehinting | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177674@lists.php.net to get a copy of this message | ||
The following code posted has a problem when in E_STRICT mode.
------------------------------------------------------------------
For PHP 5.3 version and namespaces support.
<?php
function phpErrorHandler( $code, $message, $file, $line ) {
if ( error_reporting() & $code ) {
if ( $code == E_RECOVERABLE_ERROR ) { // Scalar Type-Hinting patch.
$regexp = '/^Argument (\d)+ passed to (.+) must be an instance of
(?<hint>.+), (?<given>.+) given/i' ;
if ( preg_match( $regexp, $message, $match ) ) {
$given = $match[ 'given' ] ;
$hint = end( explode( '\\', $match[ 'hint' ] ) ) ; //
namespaces support.
if ( $hint == $given ) return true ;
}
}
return false ;
}
}
set_error_handler( 'phpErrorHandler' ) ;
/************************************/
function typeHintTest( integer $arg1 ) {
print_r( $arg1 ) ;
}
typeHintTest( true ) ; // Error throw because not integer type.
?>
-------------------------------------------------------------------
I changed it to the following, which ensures that $exploded (we had directly passed it to end
before) get's passed as a variable, by reference, and does not throw an error any longer.
function phpErrorHandler($code, $message, $file, $line )
{
/**
* PHP is dumb and doesn't allow scalar type-hinting by default, even with PHP 5.3.
*
* This is a workaround.
*/
if ( error_reporting() & $code ) {
if ( $code == E_RECOVERABLE_ERROR ) { // Scalar Type-Hinting patch.
$regexp = '/^Argument (\d)+ passed to (.+) must be an instance of
(?<hint>.+), (?<given>.+) given/i' ;
if ( preg_match( $regexp, $message, $match ) ) {
$given = $match[ 'given' ] ;
$exploded = explode( '\\', $match[ 'hint' ] );
$hint = end( $exploded ) ; // namespaces support.
if ( $hint == $given ) return true ;
}
}
return false ;
}
}
set_error_handler( 'phpErrorHandler' ) ;
----
Server IP: 69.147.83.197
Probable Submitter: 71.36.175.246
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit -- https://master.php.net/note/edit/102827
Del: integrated -- https://master.php.net/note/delete/102827/integrated
Del: useless -- https://master.php.net/note/delete/102827/useless
Del: bad code -- https://master.php.net/note/delete/102827/bad+code
Del: spam -- https://master.php.net/note/delete/102827/spam
Del: non-english -- https://master.php.net/note/delete/102827/non-english
Del: in docs -- https://master.php.net/note/delete/102827/in+docs
Del: other reasons-- https://master.php.net/note/delete/102827
Reject -- https://master.php.net/note/reject/102827
Search -- https://master.php.net/manage/user-notes.php