The PHP-FIG has such a description of FilesSHOULDeitherdeclaresymbols (classes, functions, constants, etc .) orcauseside-effects (e. g. generateoutput, change. inisettings, etc .) butSHOULDNOTdo... the PHP-FIG in the PSR-1 has such a description
Files shoshould either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change. ini settings, etc.) but shoshould NOT do both.
Where
Side-effects (e.g. generate output, change. ini settings, etc .)
Please give me your advice
Reply content:
The PHP-FIG in the PSR-1 has such a description
Files shoshould either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change. ini settings, etc.) but shoshould NOT do both.
Where
Side-effects (e.g. generate output, change. ini settings, etc .)
Please give me your advice
Side effects, as described in this English sentence:
Side-effects (e.g. generate output, change. ini settings, etc .)
Including generating output and changing ini settings ~ I personally think that side effects refer to the code that affects the environment. In addition to the above, it also includes writing data to the database, writing data to the cache, calling interfaces with data writing, and adding counter counts ......
This specification in PSR-1 means to put programs that can produce side effects (directly written in the main program body, without any encapsulated "bare" code) and purely declarative/defined programsRespectivelyIn different files. The main reason is that all files in php will be executed as long as they are introduced. Declared programs can be introduced directly through require/include security (usually only once), and are generally irrelevant to the import sequence; the programs with side effects can be directly introduced by require/include once or multiple times, which may affect the environment and even have dependencies on the sequence. These two types of code can easily cause logical confusion. So there is such a setting.
For example, @ suchasplus
function getResult($i){ $j = $i * 2; printf("i x 2 = %ld", $j); return $j;}
This is a function declaration/definition, rather than code that produces side effects.
For example, if you want to introduce a file, count it once, and finally introduce several files for Statistics (of course there are other ways to do this, here is only for example). If you do this:
//main.php
//a.php
//b.php
Herea.php
Andb.php
Li$file_count++
It is a program that produces side effects, andfunction a
Andfunction b
It is a declarative program, so mixed is a violation of the PSR-1 design. In this regard,Compliant with specificationsThe improvement isa.php
Andb.php
In$file_count++
Single file mentioned:
//main.php
//a.php
//b.php
//file_count.php
Side effect means side effects, such
function getResult($i){ $j = $i * 2; printf("i x 2 = %ld", $j); return $j;}
Printf is side effect.
Pascal distinguishes functions from procedure, that is, functions and processes.
PHP uses functions, just as the PHP array contains tuple/list/dict.