rfc:prototypecasting

This is an old revision of the document!


Request for Comments: Prototype Argument Type Casting

Introduction

There has been much discussion about adding type-hinting for scalar values; with the largest hurdle being that “it isn't the PHP way”, especially with regard to “punishing” the consumers of libraries by putting the onus of type-casting onto the them. This proposal intends to provide an alternative to type-hinting.

With both data input from a user both via HTTP and CLI coming in as strings, we very commonly compare and type juggle strings to ints, floats and booleans with little (and well known) repercussions.

This RFC proposes the idea of specifying how arguments will be type cast, and automatically type casting them upon input into functions.

Prototype Argument Type Casting

Example Code

<?php
function addProfile( (string) $username, (int) $age, (float) $salary) {
 
}
?>

In this example, you can see that we use the familiar (type) type casting syntax within the function prototype declaration.

By using this syntax, we immediately make it familiar to developers who already understand the semantics of how this syntax works.

Implementation

  • The choice of syntax:
    • Developer recognition
    • Removes ambiguity about any relationship to type-hinting
  • All type-casting should work exactly as it does when using the casting operators normally (e.g. $foo = (int) $string;
  • The user is informed just by looking at the prototype what the function is expecting, and more importantly how it will be cast
  • The user should never be penalized (with a notice/warning) about the casting

Changelog

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