Enabling 'strict_types' globally in PHP 7

22,820

Solution 1

This is deliberately not possible, because the implementation adopted after an extremely long discussion of scalar type hints was this one: https://wiki.php.net/rfc/scalar_type_hints_v5

It introduces two modes for scalar type parameters, which both guarantee that the function receiving the parameters gets the types that it requires in its signature. However, it provides two modes for how calling code can achieve that:

  • in mode 0, it automatically validates and casts certain scalar types (e.g. int parameter will convert '123' to 123, but error on 'hello')
  • in mode 1, it requires the caller to do that validation and casting before-hand, and rejects any parameter not of the correct type (e.g. both '123' and 'hello' are rejected for an int parameter)

The choice of mode is per-file, and based on the caller of the function, because:

  • the setting needs to affect built-in functions as well as user-defined ones
  • all code that calls functions needs to be checked or updated to work correctly in mode 1, but most old code will run fine in mode 0
  • with a global setting, you could only use libraries which had been tested with both modes, or the same mode you prefer
  • files that don't declare their preferred mode need to continue to work similarly to PHP 5.x to allow existing code to run; that is only possible if the default is mode 0

From the point of view of someone writing a reusable library:

  • regardless of the setting, your functions are guaranteed to receive the parameter types requested
  • if you want to receive errors when you call functions with the wrong types, you can use mode 1 without forcing other applications and libraries to be on the same setting
  • if you want to have the automatic checks and casts of mode 0, you can do that, but still interact with other libraries and applications which run in mode 1
  • old libraries which were written before PHP 7.0 (or which needed to support both when it came out) will continue to work roughly as before, because the default mode 0 is similar to existing behaviour for built-in functions

It's therefore up to you to tell PHP which files have been written to use strict type mode, and which haven't; and the way to do this is using the declare statement.

Solution 2

PHPStorm has an inspection to help you with this:

enter image description here

Solution 3

Essentially no.

Because if you only require libraries that used strict mode you will cause an unnecessary separation of packages. Also consider the strict/weak types option is just an extra in PHP.

Share:
22,820
Paradoxis
Author by

Paradoxis

About me My name is Luke Paris, I'm a Dutch security expert working at Fox-IT. Contact and links If you wish to contact me you can do so via the contact form on my website or on my LinkedIn, you can also check out the code on Github. PGP publickey SSH publickey

Updated on July 08, 2022

Comments

  • Paradoxis
    Paradoxis almost 2 years

    I'm currently migrating my website from PHP5 to PHP7, and I've started using the strict typing feature that was added. However this requires me to begin all files with the following line:

    <?php declare(strict_types=1);
    
    // All other code here
    // ...
    

    So I was wondering, is there any way to enable strict_types globally using something like php.ini or the apache configuration file so I don't have to write this line every time, and if so how could I enable this?

  • Dave
    Dave about 5 years
    That wiki entry is an interesting read to say the least. It seems to me that it would be a good thing for packages to start using it throughout and move towards better type checking.
  • Scolopendre
    Scolopendre over 4 years
    Doesn't strictly answers the question but still helpful, thanks. (:
  • sorpigal
    sorpigal about 4 years
    It's not impossible to add a php.ini directive for turning strict types on globally. If PHP devs won't do it you can patch it yourself. Build with the linked patch and set strict_types=1 in php.ini.
  • IMSoP
    IMSoP about 4 years
    @sorpigal I never said it was impossible to implement; I said after a long (and incidentally often heated) debate, it was decided that leaving it in the control of the caller was the best idea. If you're going to patch PHP, you could patch it to halt-and-catch-fire whenever someone used an incorrect type if you wanted, but that wouldn't be the same feature.
  • James
    James over 3 years
    @Scolopendre I see the type of pun you made there.
  • jave.web
    jave.web over 2 years
    @IMSoP Thank you, nice explanation, but it ensured me there should be such option, although I worry the libraries' compatibility would be the downfall of this ideal. That is a very valid argument indeed (dltdcmmnt2rdcspc)
  • IMSoP
    IMSoP over 2 years
    @jave.web Maybe it would have been ideal if PHP had worked the strict_types=1 way 20 years ago, along with some better cast behaviour so that (int)'hello' was an error, not 0. But adding a global setting, either now or back when 7.0 was released, would solve very little, and damage a lot.
  • jave.web
    jave.web over 2 years
    @IMSoP I meant ideal as a noun - that's why I called it ideal (like ideology). Otherwise I agree it supports what I said. (and yes, ideals can have bad consequences)