rfc:splweaktypehintingwithautoboxing

This is an old revision of the document!


Request for Comments: How to write RFCs

Introduction

This rfc tries to propose an alternative solution for type hinting in PHP. Basically its a weak type hinting(auto type conversion if possible without data loss), with support for scalars*, implemented as an spl interface.

Why weak type hinting?

Why support type hinting for scalars?

Why should we throw Exteption instead of trigger_error?

From the perspective of the API developer, E_STRICT or similar low priority notices cannot guarantee, that the API consumer is noticed about the invalid argument. From the perspective of the API consumer, for catching the E_STRICT is either require adding/modifying a global error handler, or using the @ to suppress the error and call error_get_last to check for error, which is just hackish.

Proposal

Currently, there is no support for hinting scalar types, and only strict type hinting is allowed (there is no conversion). PHP Catchable fatal error: Argument 1 passed to testInt::construct() must be an instance of SplInt, integer given, ... PHP Catchable fatal error: Argument 1 passed to testArray::__construct() must be an instance of ArrayObject, array given, ... The idea is that we could introduce a new Spl interface, which supports the following behaviour: - if the hinted argument type and the passed argument type is different, but the type conversion can be done without data loss, then convert the argument into the hinted type. - if the hinted argument type and the passed argument type is different, and cannot be converted without data loss, then throws a InvalidArgumentException. ===== Examples ===== TODO ===== Some crazy thoughts ===== If an object implements ArrayAccess spl interface, it could be handled as an array(eg. if you hinted array, you can pass ArrayObject).

rfc/splweaktypehintingwithautoboxing.1275319896.txt.gz · Last modified: 2017/09/22 13:28 (external edit)