Archive for the 'Type Hinting' Category

PHP 7 Scalar Variable Type Hinting

php7WARNING: The following program only works with PHP 7 installed. After getting my first PHP 7 development platform up and running on one of my Macs, I thought it’d be a good idea to do a Q & D post on working with typed (or code hinted) scalar variables. It turns out that the type hinting with scalar variables has some interesting consequences.

Not to oversell the idea of type hinting in scalar variables, but they are important for maintaining a well-functioning program; especially one using OOP and design patterns. In this simple example, I created a class with four methods. Each of the four methods illustrate a different type of scalar type hinting; float, int, string and bool. As the most common types of variable, you can add them to the existing categories of hinted type: array, and named class/interface.

A Class to Show Off Typed Scalar

The following class uses static typing and a lambda function to illustrate that scalar variable type hinting can be used in any OOP context. Each of the first three methods, calls the next method so that you can see how all four of the scalar types work:

<?php
class Scalar
{
    private static $scalar1,$scalar2,$scalar3,$scalar4;
    //client request
    public static function request(float $num)
    {
        //float
	self::$scalar1 = $num;
        echo self::$scalar1 * 5 . '<br />';
        self::noFloat(4.1);
    }
 
    private static function noFloat(int $singular)
    {
        //int
	self::$scalar2 = $singular;
        echo self::$scalar2 * 5 . '<br />';
        self::talker("Hello ");
    }
 
    private static function talker(string $word)
    {
        //string
	self::$scalar3 = $word;
        echo self::$scalar3 . " world!". '<br />';
        self::truth((6 > 7));
    }
 
    private static function truth(bool $fact)
    {
        //bool
	self::$scalar4 = $fact;
        $result=function(bool $x) { return $x ? "It is true" : "Not true!";};
        echo $result(self::$scalar4);
    }   
}
Scalar::request(4.1);
?>

The initial request calls for a floating point value (float) and uses 4.1 as an argument value. The result shows 20.5. However, in the noFloat() method, an int type is used and the returned value is 20 since the argument (4.1) is automatically changed to an integer (4)–with values always rounded down. The string is a simple string (“Hello “) and the Boolean method’s, truth(), argument is the expression (6 > 7), which is false. Note also, that in the lambda function (anonymous function) assigned to the local variable $result, also has a bool data type hint. So, the type hint bool is used twice in the truth() method. By using a simple lambda function, you can better see the truth value of the Boolean.

This short post is simply to illustrate how the new scalar type hints can be used in an OOP class that includes both static and local variables as well as a lambda function. There’s more to the new PHP 7 typing that will be touched on in the near future.

Share