PHP Classes

How Can PHP Strict Types Help Preventing Bugs When Migrating to PHP 7 or PHP 8 - Stern PHP Type Safety package blog

Recommend this page to a friend!
  All package blogs All package blogs   Stern PHP Type Safety Stern PHP Type Safety   Blog Stern PHP Type Safety package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can PHP Strict Ty...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Package: Stern PHP Type Safety

Strict type support allows PHP to check if the types of values passed to functions are of the expected type.

This possibility makes it easier for developers to catch bugs in code that pass the wrong types of values to strict functions.

Strict type support was introduced in PHP 7. To make older projects take advantage of strict types, you need to migrate those projects to PHP 7 to take advantage of this and other PHP 7 features.

In this tutorial, you will learn how to perform a smooth migration of older projects to PHP 7, so you can use strict types without introducing backwards incompatible changes that could cause more bugs to appear in your projects.




Loaded Article

Introduction

In this tutorial you will learn about:

What are PHP Strict Types?

Why You May Need to Use PHP 7 Strict Types in Your PHP Projects?

How Can You Smoothly Migrate to Use PHP 7 Strict Types in Your PHP Projects?


Go Here to Find PHP Jobs Remote Worldwide.

What are PHP Strict Types?

Strict typing is a feature introduced in PHP 7 to help developers create more robust PHP projects.

It allows you to create PHP functions that have the types of the argument values checked at run time to be able to verify if the types are correct.

Before PHP 7, it was already possible to declare functions with type declarations for array or class arguments like this:

   function someFunction(array $array_argument)
   {
     // ... Your function code goes here.
   }

PHP 7 also made possible to declare functions that require that the arguments have scalar types like integer, string, float, bool:

   function someFunction(int $integer_argument)
   {
     // ... Your function code goes here.
   }

To enable strict typing when running code in PHP 7, you need to add a declaration at the top of the PHP scripts that require strict type checking like this:

   <?php

   declare(strict_types = 1);

   // ... Your code goes here.

Why You May Need to Use PHP 7 Strict Types in Your PHP Projects?

Historically PHP has been very tolerant of programmer mistakes. For instance, you can pass values to function arguments without having the values types checked when the functions are called.

This is good and bad. It is good because you do not need to type so much code as in strict typeed languages, like Java for instance.

It can be bad because if you have a bug that makes a value of a wrong type be passed to a function, the bug may go unnoticed for a long time until you realize that the bug has bad consequences, like for instance incorrect information being stored in a database.

Being able to detect when wrong type values are passed to a function may help you find and fix bugs sooner.

This will help you create code that will have a greater quality and take less time to develop that code.

How Can You Smoothly Migrate to Use PHP 7 Strict Types in Your PHP Projects?

Migrating your code to take advantage of PHP 7 strict types is easy.

You can just add type declarations to function arguments that did not have those types defined, like those described above.

Then you need to add a declaration of strict_types option at the top of your PHP scripts.

This is fine, but if you add strict type checking to all your functions, you may need to have a lot more work to fix the code that is calling those functions.

This becomes a bigger problem when you are developing code for other developers. If they did not migrate their code to be ready to call strict typed functions, their code will stop working.

A better approach to add strict type checking is to do it progressively. Let's take a look at the steps to do this.

  1. Add Strict Versions of Your Functions to Your Code

    This means that you can add another version to each function in your code that imposes strict type checking.

    The original function would still be there but would not impose strict type checking.

    Then you can make the non-strict version of the function to call the strict version of that function.

    Let's take a look at an example to make this clear for you:

    function someFunction($integer_argument)
    {
      someFunction( ( int ) $integer_argument);
    }
    
    function strictSomeFunction(int $integer_argument)
    {
      // ... Your function code goes here.
    }
    
  2. Notify in Advance Developers of That Use Your Code About the Stricter Migration

    Once you make these changes you can tell the developers that you are evolving your code to be stricter.

    Make sure you tell them that the evolution is to help making their code more robust.

    You should also give them some time to migrate their code, like for instance 6 months.

  3. Make All The Code Strict

    After the time that you gave those other developers to migrate their code, you can make all the code strict by adding type checking to all your code.

    So your functions would look like this:

    function someFunction(int $integer_argument)
    {
      someFunction($integer_argument);
    }
    
    function strictSomeFunction(int $integer_argument)
    {
      // ... Your function code goes here.
    }
    
  4. Simplify Your Code Rewrite Effort using the Stern PHP Type Safety package

    As you may have seen, these steps are simple, but if you want to evolve classes with many functions, it is a lot of work to do this maintenance effort on your code.

    This is a matter that the Stern PHP Type Safety package can help you. It provides a trait that you can use to make all calls to the original functions be forwarded to the strict versions of your class functions.

    So a class that you want to evolve would look like this:

    <?php
    
      declare(strict_types=1);
    
      namespace YourVendor\YourNamespace;
    
      class YourClassThatUsesStrictTypes
      {
        use \ParagonIE\Stern\SternTrait;
    
        public function strictFoo(string $param = ''): bool
        {
          // ... Your function code goes here.
        }
      };
    

Basically this is it. Have a happy migration of your code to work better with newer PHP versions.

About the Stern PHP Type Safety Package

The package Stern PHP Type Safety is one of the few PHP packages that was considered notable recently because it does something that is worth paying attention.

The basic purpose is: Make class function calls invoke strict versions

Here follows in more detail what it does:

This package can make class function calls invoke strict versions.

It provides a trait that can be used by any class, so when missing functions of the class are called, it can forward the calls to functions with the prefix strict, so PHP 7 based strict type checking of the parameters values passed to the class is performed.

Conclusion

The Stern PHP Type Safety can be downloaded from download page or be installed using the PHP Composer tool following instructions in the Composer install instructions page.

This package was considered notable for implementing its benefits in a way that is worth noticing.

Notable PHP packages can be often considered innovative. If this package is also innovative, it can be nominated to the PHP Innovation Award and the author may win prizes and recognition for sharing innovative packages.

If you also developed your own notable or innovative packages consider sharing them, so you can also earn more visibility for your package as well nice prizes.

One nice prize that many PHP developers want and you may like is the PHP elePHPant mascot plush.




You need to be a registered user or login to post a comment

1,614,687 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Stern PHP Type Safety Stern PHP Type Safety   Blog Stern PHP Type Safety package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can PHP Strict Ty...