php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68126 syntax for optional definition of function return type
Submitted: 2014-10-01 19:13 UTC Modified: 2015-02-25 01:14 UTC
From: lefevre dot 10 at osu dot edu Assigned: requinix (profile)
Status: Closed Package: *General Issues
PHP Version: 7.0 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lefevre dot 10 at osu dot edu
New email:
PHP Version: OS:

 

 [2014-10-01 19:13 UTC] lefevre dot 10 at osu dot edu
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.
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-01 19:43 UTC] requinix@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2014-10-01 19:43 UTC] requinix@php.net
Keep an eye on https://wiki.php.net/rfc/returntypehinting
 [2014-10-15 16:03 UTC] tyrael@php.net
-PHP Version: 5.6.0 +PHP Version: 7.0
 [2014-10-15 16:03 UTC] tyrael@php.net
-Operating System: Linux +Operating System:
 [2015-02-25 01:07 UTC] edgar dot r dot sandi at gmail dot com
Hi

this feature has been implemented in PHP 7
https://wiki.php.net/rfc/return_types

Edgar R. Sandi
cheers
 [2015-02-25 01:14 UTC] requinix@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: requinix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 03:01:29 2024 UTC