[PHP-BUG] Req #68126 [NEW]: syntax for optional definition of function return type
[email protected] ("lefevre dot 10 at osu dot edu")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
From: lefevre dot 10 at osu dot edu
Operating system: Linux
PHP version: 5.6.0
Package: PHP Language Specification
Bug Type: Feature/Change Request
Bug description:syntax for optional definition of function return type
Description:
------------
It would be useful to optionally be able define a return type for a
function, perhaps like so:
function foo($arg1, $arg2) returns array { ... }
Currently the Code by Contract principle is not fully implementable in
interfaces because there is no way to guarantee a return type for
functions, or even any return value at all.
Adding a return type would allow one to do better code by contract:
interface iMyInterface {
function handle_data($arg1) returns iMyResponse;
}
interface iMyResponse { ... }
When writing code that implements or depends on iMyInterface, I can know
what type I will be getting out of the handle_data method.
Optionally one could specify multiple return types:
function my_string_function($string) returns string, boolean {...}
As the author of the function, I will know if I have a logic error when
this function I've written doesn't return the pre-defined types and I
get an error.
As a client programer (who will later use the above function, but hasn't
written it), I will know at a glance what output I can expect from a
function, without scanning through the body to deduce it.
For backwards compatibility, flexibility and php-ishness, I don't think
this should be required, but optional.
function my_string_function_1() {...}
function my_string_function_2() returns string, boolean {...}
function my_string_function_3() returns string, boolean, array {...}
Test script:
---------------
<?php
function my_string_function($string) returns array, string, boolean {
// do nothing; syntax presently invalid.
}
--
Edit bug report at https://bugs.php.net/bug.php?id=68126&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68126&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68126&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68126&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68126&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68126&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68126&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68126&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68126&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68126&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68126&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68126&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68126&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68126&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68126&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68126&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68126&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68126&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68126&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68126&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68126&r=mysqlcfg