Skip to content

Instantly share code, notes, and snippets.

@dlsniper
Last active December 18, 2015 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlsniper/5863012 to your computer and use it in GitHub Desktop.
Save dlsniper/5863012 to your computer and use it in GitHub Desktop.
PHP RFC for function / method return typing

Request for Comments: Method Return Typing

  • Version: 0.1
  • Date: 2013-06-26
  • Author: Florin Patan
  • Status: In draft
  • Patch included: NO

Introduction

The scope of this RFC is to add support for function / method return typing system.

Given the fact that more and more people use PHP every time they want to start working on their applications and that PHP is no longer used only for simple website creations, code quality should come to mind when creating those applications.

As such, one of the missing features of PHP is a return typing system which would allow users to specify a strict return type for functions and methods.

This feature would allow for improvements in the produced code quality as once you declare the return type of a method in a interface for example.

Third party tools could also benefit from this as they could try and track the return type and provide code insights when the returned value doesn't match the specified type.

Further improvements could be possible as knowing the returned type could be useful for a multi-level opcode cache system.

Syntactical Implementation

Approach

The proposed structure would look like this:

[method_attributes] function [<return_type>] [method_name] '(' parameters ')'

Allowed return types

Basically we should allow for every type in php to be returned, not just the types allowed for parameter typing. As such, the following types should be supported out of the box:

  • boolean
  • int
  • float
  • string
  • array
  • object
  • resource
  • NULL
  • callable
  • interface
  • class

If no return type is specified the the parser should act like it acts currently, ignoring any typing.

Implicit conversion

There should be no implicit conversion allowed.

Although this is contrary to PHP type juggling, having return typing is meant to prevent this juggling as well.

Example Implementations

function <int> add($a, $b) {
    return (int) ($a + $b);
}

interface DemoInterface {
   public function <mysqli> getDriver();
}

class Demo implements DemoInterface {
   public function <mysqli> getDriver() {
        return new mysqli('localhost', 'my_user', 'my_password', 'my_db');
   }
}

Notice

Strictly relying on return type while using libraries outside of user control could result in having serious code issues so when used for direct usage. As such, the best way to use this would be in combination with parameter hinting.

Advantages

  • Improves code quality.
  • Improves tooling support.
  • Possible further improvements in data handling / code speed execution.

Disadvantages

  • Minor/negligible performance impact when used
  • Not performing additional checks in userland could render to serious issues in code when relying to libraries outside of user control.
  • Possible other changes caused by changing the parser

Tests

None yet.

Patch

None yet.

Credits

This proposal is inspired from previous work of Will Fitch will.fitch@gmail.com which can be found here: https://wiki.php.net/rfc/returntypehint2

Changelog

  • 0.1 Initial proposal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment