PHP Augmented Types

/**
* @param int $a
* @return float[]
*/
function foo ($a) {
    echo "You passed in the integer $a";
    return [$a * 1.0, $a * 2.718];
}

Augmented Types simply provides a way of enforcing argument and return types on function calls in PHP. It parses a given function’s type information from phpDoc-style type annotations (based on the phpDocumenter project), and it stores this information efficiently for later enforcement during runtime.

As PHP typehints (in functions) are limited to objects and arrays, this might improve coding indeed (especially in big teams).

Alternative could be to introduce a few classes for the basic types. Think Objective-C, where NSInteger contains an int for example. That way you could force it without the need for a runtime extension. The custom types will of course create overhead, yet that’s something that needs to be benched before dismissing the idea entirely.

Making PHP Safer: Introducing Augmented Types →

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.