note 83442 added to language.oop5.typehinting

From: Date: Mon, 26 May 2008 18:00:05 +0000
Subject: note 83442 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-139799@lists.php.net to get a copy of this message
People often ask about scalar/basic typehints. Here is a drop in class that I use in my MVC framework that will enable typehints through the use of a custom error handler. Note: You should include this code above all other code in your include headers and if you are the using set_error_handler() function you should be aware that this uses it as well. You may need to chain your set_error_handlers() Why? 1) Because people are sick of using the is_* functions to validate parameters. 2) Reduction of redundant coding for defensive coders. 3) Functions/Methods are self defining/documenting as to required input. Also.. Follow the discussion for typehints in PHP 6.0 on the PHP Internals boards. <?php define('TYPEHINT_PCRE' ,'/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/'); class Typehint { private static $Typehints = array( 'boolean' => 'is_bool', 'integer' => 'is_int', 'float' => 'is_float', 'string' => 'is_string', 'resrouce' => 'is_resource' ); private function __Constrct() {} public static function initializeHandler() { set_error_handler('Typehint::handleTypehint'); return TRUE; } private static function getTypehintedArgument($ThBackTrace, $ThFunction, $ThArgIndex, &$ThArgValue) { foreach ($ThBackTrace as $ThTrace) { // Match the function; Note we could do more defensive error checking. if (isset($ThTrace['function']) && $ThTrace['function'] == $ThFunction) { $ThArgValue = $ThTrace['args'][$ThArgIndex - 1]; return TRUE; } } return FALSE; } public static function handleTypehint($ErrLevel, $ErrMessage) { if ($ErrLevel == E_RECOVERABLE_ERROR) { if (preg_match(TYPEHINT_PCRE, $ErrMessage, $ErrMatches)) { list($ErrMatch, $ThArgIndex, $ThClass, $ThFunction, $ThHint, $ThType) = $ErrMatches; if (isset(self::$Typehints[$ThHint])) { $ThBacktrace = debug_backtrace(); $ThArgValue = NULL; if (self::getTypehintedArgument($ThBacktrace, $ThFunction, $ThArgIndex, $ThArgValue)) { if (call_user_func(self::$Typehints[$ThHint], $ThArgValue)) { return TRUE; } } } } } return FALSE; } } Typehint::initializeHandler(); ?> An are some examples of the class in use: <?php function teststring(string $string) { echo $string; } function testinteger(integer $integer) { echo $integer; } function testfloat(float $float) { echo $float; } // This will work for class methods as well. ?> You get the picture.. ---- Server IP: 194.153.94.10 Probable Submitter: 75.68.94.74 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/83442 Del: integrated -- https://master.php.net/note/delete/83442/integrated Del: useless -- https://master.php.net/note/delete/83442/useless Del: bad code -- https://master.php.net/note/delete/83442/bad+code Del: spam -- https://master.php.net/note/delete/83442/spam Del: non-english -- https://master.php.net/note/delete/83442/non-english Del: in docs -- https://master.php.net/note/delete/83442/in+docs Del: other reasons-- https://master.php.net/note/delete/83442 Reject -- https://master.php.net/note/reject/83442 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139799) next »