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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图