Table of Contents

RFC: Prototype Argument Type Casting

Introduction

This proposal is in no way related to scalar 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) $name, (int) $age, (float) $heightInMeters) {
    echo gettype($name);   // "string"
    echo gettype($age);    // "integer"
    echo gettype($heightInMeters); // "double"
}
 
addProfile("Davey Shafik", "27", "1.77");
?>

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

Changelog