rfc:prototypecasting
RFC: Prototype Argument Type Casting
- Version: 0.1
- Date: 2012-02-27
- Author: Davey Shafik davey@php.net
- Status: In Progress
- First Published at: http://wiki.php.net/rfc/prototypecasting
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
- 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.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1