PHP Classes

File: example/withReachingLimit.php

Recommend this page to a friend!
  Classes of nvb  >  PHP Memory Limit Manager  >  example/withReachingLimit.php  >  Download  
File: example/withReachingLimit.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Memory Limit Manager
Set the PHP memory limit and check if is exceeded
Author: By
Last change:
Date: 2 years ago
Size: 830 bytes
 

Contents

Class file image Download
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-07-27
 */

require_once __DIR__ . '/../vendor/autoload.php';

echo
'starting example' . PHP_EOL;

$manager = new Net\Bazzline\Component\MemoryLimitManager\MemoryLimitManager();

$currentMemoryUsageInBytes = memory_get_usage(true);

$manager->setBufferInMegaBytes(0);
$manager->setLimitInBytes($currentMemoryUsageInBytes);

$data = array();

for (
$iterator = 0; $iterator < 10; ++$iterator) {
   
$data[$iterator] = true;
    if (
$manager->isLimitReached()) {
        echo
'error - memory limit of ' . $manager->getLimitInBytes() . ' bytes reached (current usage: ' . $manager->getCurrentUsageInBytes() . ' bytes)' . PHP_EOL;
        exit(
1);
    }
}

echo
'finished example with memory usage of ' . $manager->getCurrentUsageInMegaBytes() . ' mb' . PHP_EOL;