Req #67210 [Opn->Nab]: This is a core php feature update request
| From: | johannes@php.net | Date: | Mon, 05 May 2014 14:57:32 +0000 |
| Subject: | Req #67210 [Opn->Nab]: This is a core php feature update request | ||
| References: | 1 | Groups: | php.bugs php.qa |
| Request: | Send a blank email to php-bugs+get-185638@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67210&edit=1
ID: 67210
Updated by: johannes@php.net
Reported by: mrdaniellee2020 at gmail dot com
Summary: This is a core php feature update request
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Testing related
Operating System: All
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Return type handling is discussed as part of this RFC: https://wiki.php.net/rfc/returntypehinting
No need for extra bug tracker item.
Previous Comments:
------------------------------------------------------------------------
[2014-05-05 14:24:04] mrdaniellee2020 at gmail dot com
Description:
------------
Hi,
Earlier today i was reading about the newly release Facebook Hack Languages and Tools online with
this they have added a few new features which are quite interesting.
However there was one feature they added which i had already thought of in the past which i thought
would be excellent if php had this installed by default.
Its literally to check return values of methods and classes in order to check returned values just
as easily as you can check input values.
I wrote a quick class explaining exactly this.
I think it would be useful to have by default in php as i think it would help to trim down php unit
testing massively.
All this class simple does is allow you to check the response / return value of a function to ensure
the correct data type is returned, otherwise it throws an exception.
Please review my test script below and tell me what you think?
Im sure if this was placed in the core the actual syntax of the call could be shortend to something
like return('string', '1234'); or similar.
Test script:
---------------
<?php
class Check {
/* description
* This class is used to check function return types *
/* usage */
//return Check::type('string', $data);
/* supported return types
is_array
is_âbool
is_âcallable
is_âdouble
is_âfloat
is_âint
is_âinteger
is_âlong
is_ânull
is_ânumeric
is_âobject
is_âreal
is_âresource
is_âscalar
is_âstring
*/
//set allowed return types
public static $types =
array('array','bool','callable','double','float','int','integer','long','null','numeric','object','real','resource','scalar','string');
//check return type
public static function type($type, $value, $allowNull = false) {
//check an allowed type is selected
if(!in_array($type, self::$types)) {
throw new Exception('return type not listed');
}
//check for $allowNull / empty values
if($allowNull == true) {
if($value == null || $value == false || $value == '') {
return $value;
}
}
//prepare method name used to check
$function = 'is_'.$type;
//check return type
if(!$function($value)) {self::error($type, $value);}
//return value
return $value;
}
//throw generic error for failed return type
public static function error($type = '', $value = '') {
$backtrace = debug_backtrace();
$error = 'failed method return type found in '.$backtrace[0]['file'].'
at line '.$backtrace[0]['line'].' ';
$error .= 'return type expected: '.$type.' - ';
$error .= 'actual type returned: '.gettype($value).' - ';
$error .= 'containing: '.json_encode($value);
throw new Exception($error);
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67210&edit=1