Tag Archive for 'PHP 7'

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:

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

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.


PHP 7 / PHPNG: Speed and Better Data Typing

PHP7Hello PHP7

Last Wednesday (11/11/15), we drove up from Connecticut to the Boston PHP meeting where Zend’s Cal Evans gave a very informative talk on PHP 7 at the Microsoft New England Research and Development (N.E.R.D.) center next to the MIT campus. (PHP 7 is also known as PHPNG with NG = “Next Generation”.) It was well-worth the 200 mile (332K) round trip as my compadre (a Drupal guy) and I were examining every nuance of PHP 7 and what it meant for design patterns, functional programming and other PHP-based apps like Drupal and Word Press. The short version is that it means better speed. Fortunately, most of the other new features extend what we’ve been doing on this blog since its inception; namely, using mysqli instead of mysql; creating constructor functions using __construct instead of the class name; and using type hinting where possible.


I have not worried about speed for years. The bandwidth and processor speeds have regularly increased to the point where structure was more important than speed to me. Besides, there wasn’t much I could do about multi-core programming since PHP does not support concurrent development. (Maybe in version 8). I’ve already had a rant about my hosting service on my other blog; so I’ll not repeat it here; but get a hosting service that supports PHP 7.

So what about speed? It’s roughly twice what it used to be. Figure 1 shows some early timing data (which varies all over the place depending on the app you’re using). However, it does show how the speed since PHP 5.0 increased significantly. For the sake of argument, let’s say it’s about twice as fast as PHP 5.6.

Figure 1: Speed Comparisons with PHP 5.x and PHP 7

Figure 1: Speed Comparisons with PHP 5.x and PHP 7

If you’re doing professional development, you’ll want to wait until the official release of PHP 7 is out, but once it is, get yourself and your clients on a hosting service that supports PHP 7.

What Happened to PHP 6 : Abandonware

One of the questions asked at the presentation by Cal Evans was,

What happened to PHP 6?

I was thinking “vaporware” but that’s not fair nor accurate. Cal said that it was getting so on-off-on-off for a release, that it was simply abandoned to reduce confusion; so PHP 6 is “abandonware” but in fact about the only thing abandoned was the name. At the last minute one of the most important changes occurred when a huge speed improvement was introduced. Zend’s PHPNG (PHP Next Generation) engine is in head-to-head competition with HHVM (Hip-Hop Virtual Machine) from Facebook; so now with two engines competing for speed, let’s hope we all win in the speed of PHP. (Most of the speed data is from benchmarks based on the speed of numeric loops; so while speed is going to improve; don’t expect a rocket to the moon on most speed improvements.)

So What Effect Does this Have on OOP and Design Patterns?

From what I can tell; some affects to OOP may crop up, but not a lot as far as this blog is concerned. All of the examples on this blog have kept data typing strict: once a variable uses a data type, it doesn’t change. Likewise, type-hinting in parameters have forced only certain data types to be allowed in parameters. With PHP 7, type declarations are now available for scalar variables and return types. You should be able to add these declarations to any of the examples on this blog without an exception being thrown. All along we’ve been careful not to switch data types. One exception might be between integer and float types; so you might want to check there. Also, PHP 7 has a new integer divide function (intdiv()). The interval() function used up to this point will likely be replaced by the new intdiv() function where appropriate.

One of the new features that will definitely affect the way methods are written is the addition of return-type declarations. These work like the parameter declarations that have been used with object type (e.g., array, classes). So, for example, you can have something like the following:

class Infomaster
   private $members = 32;
   public function getMembers(): int
       return $groups;

The intdiv() function insures that the value will be an integer and the return type (int) is guaranteed. Throughout this blog, you should be able to use these kinds of type declarations without disrupting the code since care has been taken to be certain that everything is equivalent to a more strongly typed language.

What’s Next?

I’ve been waiting for the official release of PHP 7 before going ahead and finish my Sandlight CMS. This is for two reasons. My current Sandlight site is on a hosting service that is pretty far behind the curve in terms of current PHP and MySql. (e.g., I cannot use mysqli because their version of MySql is so out of date.) I’ve got to get a new hosting service, and unfortunately, the best one for what I want to do requires me to step up my IT Networking skills—get better at the command lines to work with installing PHP and the rest of LAMP files on a server that is basically carte blanche. Once I get to the point where I can program and develop, I’m good. It’s just the set-up that gives me the heebie-jeebies.

The other reason is that I want to use PHP 7 as a development language. I’m not going to worry about backward compatibility since I don’t have to support any lame web sites that some hacker built. Everything is going to remain both OOP with an increasing introduction of Functional Programming techniques that can be handled by PHP 7. I may use some Hack Language and install HHVM, but I prefer to use PHPNG with PHP 7. If I can, I’ll use both together. First, though, I have to go to the salt mines of the IT moles and get my system set up. Groan.