note 100550 deleted from language.oop5.typehinting by nikic
| From: | nikic@php.net | Date: | Sun, 01 Nov 2020 21:51:44 +0000 |
| Subject: | note 100550 deleted from language.oop5.typehinting by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215692@lists.php.net to get a copy of this message | ||
Note Submitter: alejosimon at gmail dot com
----
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.
?>