PHP-FIG 中 PSR-1 有这样一段描述
Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
其中
side-effects (e.g. generate output, change .ini settings, etc.)
不明其意, 请高手们赐教
回复内容:
PHP-FIG 中 PSR-1 有这样一段描述
Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
其中
side-effects (e.g. generate output, change .ini settings, etc.)
不明其意, 请高手们赐教
副作用,就如这句英文所说:
side-effects (e.g. generate output, change .ini settings, etc.)
包括产生输出、改变ini设置等等~ 我个人认为副作用就指对环境产生影响的代码。除了上面说的,还包括写入数据库、写入缓存、调用带数据写入的接口、增加计数器计数等等等等……
PSR-1里这条规范的意思是,把能产生副作用的程序(直接写在主程序体中的、没有做任何封装的 “裸”代码)和纯声明/定义性的程序分别放在不同的文件里。主要原因是,php里所有的文件只要引入就会被执行。纯声明的程序可以直接通过require/include安全引入(通常只能引入一次),而且通常与引入顺序无关;而带副作用的程序直接require/include引入可以一次或多次,且会影响环境,甚至有对顺序的依赖。这两种代码混在一起容易导致逻辑混乱。所以有这么一条设置。
而像@suchasplus 举例的
function getResult($i){ $j = $i * 2; printf("i x 2 = %ld", $j); return $j; }
这种情况属于函数声明/定义,不是会产生副作用的代码。
另例如,如果需要引入一个文件就计数一次,最终统计一共引入了几个文件(当然有其他方法可以做到这个功能,这里仅供举例),如果你这么做:
//main.php <?php $file_count = 0; require 'a.php'; require 'b.php'; echo $file_count; ?> //a.php <?php function a(){ //... } $file_count ++; ?> //b.php <?php function b(){ //... } $file_count ++; ?>
这里,在a.php
和b.php
里 $file_count++
就属于会产生副作用的程序,而 function a
和function b
则属于声明性程序,如此混用就属于违反PSR-1的设计。对此,一种符合规范的改进则是将a.php
和b.php
里的$file_count++
提到单独文件:
//main.php <?php $file_count = 0; require 'a.php'; require 'count_file.php'; require 'b.php'; require 'count_file.php'; echo $file_count; ?> //a.php <?php function a(){ //... } ?> //b.php <?php function b(){ //... } ?> //file_count.php <?php $file_count ++; ?>
side effect就是副作用的意思 比如
function getResult($i){
$j = $i * 2;
printf("i x 2 = %ld", $j);
return $j;
}
printf这句话就是side effect
Pascal中有function和procedure的区分, 即函数和过程
PHP用function都包含了, 就像PHP的array包含了tuple/list/dict一样

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
