Php-fig in PSR-1 have such a description
Files should either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, Cha Nge. ini settings, etc.) But should does both.
which
Side-effects (e.g. generate output, change. ini settings, etc.)
It is unclear, please enlighten the experts
Reply content:
Php-fig in PSR-1 have such a description
Files should either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, Cha Nge. ini settings, etc.) But should does both.
which
Side-effects (e.g. generate output, change. ini settings, etc.)
It is unclear, please enlighten the experts
Side effects, as this sentence says in English:
Side-effects (e.g. generate output, change. ini settings, etc.)
including generating output, changing INI settings, and so on ~ I personally believe that side effects are code that affects the environment. In addition to the above, it also includes writing to the database, writing to the cache, calling interfaces with data writes, increasing counter counts, and so on ...
This specification in PSR-1 means that programs that can produce side effects ("bare" code that is written directly in the main program body without any encapsulation) and pure declaration/definition are placed in separate files. The main reason is that all the files in PHP will be executed as soon as they are introduced. A purely declarative program can be introduced directly through Require/include security (usually only once), and is usually not related to the introduction sequence, whereas a program with side effects can be introduced directly require/include one or more times, affecting the environment and even having a dependency on the order. Mixing these two types of code can lead to logical confusion. So there is such a setting.
And like @suchasplus, for example,
function getResult($i){ $j = $i * 2; printf("i x 2 = %ld", $j); return $j;}
This is a function declaration/definition, not a code that produces side effects.
For example, if you need to introduce a file to count once, the final statistic introduces a total of several files (of course there are other ways to do this function, here for example only), if you do:
//main.php
//a.php
//b.php
Here, in a.php
and in the b.php
process of $file_count++
producing side effects, function a
and is a function b
declarative procedure, so mixed is a violation of PSR-1 design. In this respect, a specification-compliant improvement would be to a.php
refer to b.php
the individual files in and out $file_count++
:
//main.php
//a.php
//b.php
//file_count.php
Side effect is a side effect, like
function getResult($i){ $j = $i * 2; printf("i x 2 = %ld", $j); return $j;}
printf, that's the word side effect.
The distinction between function and procedure in Pascal, i.e. functions and processes
PHP is included as a function, just like the PHP array contains tuple/list/dict.