0

If my controller, index.php for example, includes declare(strict_types=1); and then other PHP files are included, does the strict_types declaration apply to the included files also?

Or does the declaration need to be in every PHP file?

Dave
  • 5,108
  • 16
  • 30
  • 40
  • Depends if it came before the include or not^^ - also, did you not try? – treyBake Jun 19 '19 at 13:04
  • It has be before any includes since it must be the first executable statement. – Dave Jun 19 '19 at 13:06
  • In that case, I'd imagine so, as include really is just echo what's in file X – treyBake Jun 19 '19 at 13:06
  • Have you read the manual…? https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict – deceze Jun 19 '19 at 13:08
  • Well I should have tried it. The answer is it does not apply to the included files which I find a bit strange but that's the way it is apparently. – Dave Jun 19 '19 at 13:08
  • `It is possible to enable strict mode on a per-file basis` - I guess they expect you to do per file.. which seems odd – treyBake Jun 19 '19 at 13:10
  • I did read the manual, several times, but could not find an answer. But I should have just tried it (dumb that I didn't). Wish it could be a global setting. The manual says "It is possible to enable strict mode on a per-file basis." which seems to imply it can be enabled globally but I don't see a way to do that. – Dave Jun 19 '19 at 13:11
  • 1
    @Dave may help https://stackoverflow.com/questions/37111470/enabling-strict-types-globally-in-php-7 (or at least, help clear things up) – treyBake Jun 19 '19 at 13:12
  • Don't know why that one didn't show up in my searching but it answers the question. Guess I should close my own question as a dup :) – Dave Jun 19 '19 at 13:14
  • 1
    @treyBake Since this can greatly change runtime behaviour, of course you want to opt-in to this on a per-file basis. Imagine importing a library that's a bit loose with its typing; now you can only disable strict typing for everything, or get errors. – deceze Jun 19 '19 at 13:22

0 Answers0