In php-fig-PHP, what is side-effects?

Source: Internet
Author: User
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.phpAndb.phpLi$file_count++It is a program that produces side effects, andfunction aAndfunction bIt is a declarative program, so mixed is a violation of the PSR-1 design. In this regard,Compliant with specificationsThe improvement isa.phpAndb.phpIn$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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.