Is using ini_set in a script considered bad practice?
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.
source to share
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);
}
source to share
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]
source to share
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 :)
source to share