drngnh708353 2017-02-07 10:38
浏览 71
已采纳

什么是使用ini_set()的PSR-1兼容替代方案?

I am in the process of making my code base PSR-2 compliant (and therefore PSR-1 compliant) and I have come across the following code:

public function init()
{
    parent::init();

    // Allow A Larger PHP Memory Limit For This Script
    ini_set("memory_limit", "512M");

    // Allow A Larger Script Execution Limit For This Script
    ini_set('max_execution_time', 300);
}

Which is used to increase the amount of memory and execution time which this particular script (which is only accessible to site admin and run infrequently) is able to consume.

Setting the default memory_limit and max_execution_time in php.ini (which will be honoured by all standard scripts/files) and then increasing those limits as and when needed, certainly feels like a perfectly acceptable and logical implementation to me.

However, I remember that PSR-1 states that:

"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."

It then goes on to state that using ini_set() is an example of a side-effect (at least their example shows it being used outside the scope of a class or function):

<?php
// side effect: change ini settings
ini_set('error_reporting', E_ALL);

This documentation can be seen here

My questions are therefore:

  1. Is the use of ini_set() within a classes method (as per my context) an example of a side-effect?
  2. If so, what is the PSR-1 compliant alternative?

This particular script needs to be allowed greater memory usage and execution time, but I do not want to just increase the global php.ini settings for this as that would allow for all other PHP processes to use more memory and execution time that they should.

  • 写回答

1条回答 默认 最新

  • doujionggan9570 2017-02-07 10:46
    关注

    PSR-1 is just talking about the top-level code in the file. That top-level code should either be causing side effects or declaring things.

    In your example, you're defining a class and its methods. The side effect doesn't happen when loading the file, it happens when you call the function. To be PSR-1 compliant, that call should not be in the top-level code of this file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵