Skip to content

Instantly share code, notes, and snippets.

@jesseschalken
Last active September 11, 2015 01:14
Show Gist options
  • Save jesseschalken/522d6e6c8a3f7fd4d010 to your computer and use it in GitHub Desktop.
Save jesseschalken/522d6e6c8a3f7fd4d010 to your computer and use it in GitHub Desktop.
Class to increase/decrease PHP's memory_limit by a given amount
<?php
class MemoryLimit {
private $num = 0;
function inc($num) {
if (!$num)
return;
$this->num += $num;
$val = trim(ini_get('memory_limit'));
switch (strtolower($val[strlen($val)-1])) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
if ($val != -1) {
$val += $num;
ini_set('memory_limit', $val);
}
}
function __destruct() {
$this->inc(-$this->num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment