Is using ini_set in a script considered bad practice?

I am doing a code review and found 2 places where

ini_set('memory_limit', '512M');

      

used in a function. This is normal? Somehow it doesn't seem right to me. Is this bad practice?

Thank!

+3


source to share


7 replies


There is nothing wrong. Consider a scenario that is memory_limit

globally set in a PHP ini file and you only want to override this setting for one specific script / request to allow the operation to use more memory.



The call ini_set

to a PHP script will only have an effect when PHP fulfills that particular request.

+2


source


you can alternatively wrap this function call with another callable function, making it so that you can change the implementation if it is better in the future, or if you want to update the memory limit globally with minimal effort



function setMemory($limit = '512M') {
    ini_set('memory_limit', $limit);
}

      

+1


source


My opinion (I think you asked for an opinion):

[BEGINNING OF WORK]

Of course, you shouldn't use ini_set ('memory_limit', ....);

But I always place declarations like this at the top of my script (if memory hungry), NOT in a function ...

If you get in the habit of changing memory requirements in functions, I expect it might be difficult to figure out what the current setting is if many of those functions are called in the same script.

So, to test the code, if it happens once or twice it's not a very bad practice, but if you run into it a lot, I would raise a red flag.

[END OF OPINION]

+1


source


It's not bad. Since you may not have access to php.ini Or you only need some value in one script.

+1


source


Using a script at the start of any thought tends to be bad practice, but if you are setting properties such as memory it is not that bad if you are trying to execute other scripts.

0


source


If you are using scripts that require 512M and you do not have permission to change this parameter in php.ini, then you can use ini_set

0


source


Note : the same PHP script can be operated by multiple users
If you have 10 users running the script, your server may run out of memory: 10 X 512M = 5120M = 5G , so be careful!

Using the ini_set () function is not considered a bad parameter, but you must follow some rules to keep it clean:

Centralize all your ini_set () in one config file
custom_ini_set.php

function custom_ini_set($script_path_name_s) {
        switch ($script_path_name_s) {
            case '/dir1/sub_dir1/script1.php' : {
                ini_set('memory_limit', '512M');
                break;
            }

            case '/dir1/sub_dir2/script2.php' : {
                ini_set('memory_limit',         '256M');
                ini_set('max_execution_time',   128);
                break;
            }
            // ...
        }
    }

      

And call this function in your script that needs more resource:

require_once   ('custom_ini_set.php');
custom_ini_set (__FILE__);

      

Better to put this in XML file instead of PHP file :)

0


source







All Articles