Re: PHP header files, restrict to declaring code only
| From: | Larry Garfield | Date: | Tue, 14 May 2019 18:48:11 +0000 |
| Subject: | Re: PHP header files, restrict to declaring code only | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105706@lists.php.net to get a copy of this message | ||
On Mon, May 13, 2019, at 7:49 AM, Johannes Schlüter wrote:
> On Fr, 2019-05-10 at 15:14 +0200, Lynn wrote:
> > Hi Nikita,
> >
> > By limiting a header file to declaring code only, a couple of issues
> > could
> > be tackled:
> >
> > 1. No side effects when loading files. You can assume the global
> > state
> > remains the same, no
> > code is executed by this file, and no output can be sent.
>
> Mind that this won't be side-effect free. Declaring a class/function is
> a side-effect in itself.
>
> For dealing with syntax differences we could use the declare()
> statement, as used for strict_types or encoding.
>
> A question is whether it is worthwhile to have multiple syntaxes in
> parallel. This adds burden for developers using PHP, (i.e. copying code
> from tutorials or other files might fail sometimes etc.) tools
> processing PHP (IDEs, code analyzers, ...) and language developers (new
> syntax changes have to be evaluated for both contexts)
>
> If this were a strategy to transition to a new syntax (deprecating "old
> PHP") such a mechanism would be the way to go. For just having
> alternatives to avoid a syntax conflict cost imo is too big.
>
> johannes
Another possible place such a marker could be useful is in the new preloading functionality in 5.4.
Any file that is preloaded is going to never execute again, ever. That means preloading a file that
has both symbol declarations and executing global code could lead to weird results. I could see a
preloader that would only try to preload symbol-only files.
That said... most polyfill files, of which there are a decent number, do some variation on:
<?php
if (!function_exists('coolness')) {
function coolness(int $coolLevel) { ... }
}
?>
Which would then be incompatible with such a check, yet are perfectly valid to preload; maybe even
an especially good case for preloading. So such a strict check is probably unwise.
Maybe it would be helpful for the various experimental "precompile PHP" efforts, like what
Anthony Ferrara has been doing? I've no idea there.
I appreciate the intent here, and agree most code files should be symbol-only, but I'm not sure
there's a strong enough use case in practice for it. That said, were it to be adopted I would
strongly favor a declare rather than a different file extension or tag. Something like:
<?php
declare('strict_types=1');
declare('this file only declares symbols, error if it tries to do anything else');
class Blah { ...}
?>
That would be the least obtrusive way of adding it, I think.
--Larry Garfield