Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 104 | All time: 9,745 This week: 66 |
Version | License | PHP version | Categories | |||
circular-buffer 1.0.2 | GNU General Publi... | 5.5 | PHP 5, Data types |
Description | Author | |
This class can store values in a circular buffer array. |
<?php
|
The CircularBuffer class implements a circular buffer, ie a buffer that can store a limited number of items. If you add items that exceed the capacity of the circular buffer, then the older items will be "forgotten" to leave room to the ones you want to add.
Circular buffers are often used when you have to face the following trade-offs :
As time goes by, you can accept to discard some of the oldest data you're storing into the circular buffer
## Creating a circular buffer ##
Creating a circular buffer is easy ; just fix yourself a limit on the number of items you want to store :
$buffer = new CircularBuffer ( 1000 ) ; // The circular buffer will be able to store at most 1000 elements
Adding items is easy ; just use the circular buffer as an array to append your latest items :
$buffer [] = "This is a log message" ;
Since the CircularBuffer class implements the ArrayAccess, Coutable and IteratorAggregate interface, you can perform the following operations :
The CircularBuffer class constructor has 3 different signatures, described in the following sections :
Creates a circular buffer that is able to store $size elements.
Creates a circular buffer and initializes it with the contents of $array.
The size of the circular buffer will be the size of the specified array.
Creates a circular buffer and initializes it with the contents of $array.
The circular buffer will have $size size elements. If $size is less than the number of items in $array, the circular buffer will be initialized with its first $size elements. If $size is greater, then emty elements will be added to the circular buffer.
Empties the contents of the circular buffer.
Files (5) |
File | Role | Description |
---|---|---|
CircularBuffer.phpclass | Class | Class source |
example.php | Example | Example script |
LICENSE | Lic. | License text |
NOTICE | Data | Auxiliary data |
README.md | Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.