Examples
Examples –
Common use cases.
Getting a random fortune
<?php
require_once 'File/Fortune.php';
// Grab from a single fortune file:
$fortunes = new File_Fortune('/path/to/fortune/file');
echo $fortunes->getRandom();
// Grab from a directory of fortune files:
$fortunes = new File_Fortune('/path/to/fortune/files/');
echo $fortunes->getRandom();
?>
Get all fortunes
<?php
require_once 'File/Fortune.php';
$fortunes = new File_Fortune('/path/to/fortune/file');
$cached = $fortunes->getAll();
// or all fortunes in all files:
$fortunes->setDirectory('/path/to/fortunes/');
$cached = $fortunes->getAll();
?>
Counting fortunes
<?php
require_once 'File/Fortune.php';
$fortunes = new File_Fortune('/path/to/fortune/file');
$count = count($fortunes);
// or all fortunes in all files:
$fortunes->setDirectory('/path/to/fortunes/');
$count = count($fortunes);
?>
Looping through fortunes
<?php
require_once 'File/Fortune.php';
$fortunes = new File_Fortune('/path/to/fortune/file');
foreach ($fortunes as $fortune) {
echo $fortune;
}
?>
Note: this will raise exceptions if a directory or multiple files have been set.
Manipulating fortunes
<?php
require_once 'File/Fortune.php';
$fortunes = new File_Fortune('/path/to/fortune/file');
// Delete a fortune:
unset($fortunes[2]); // deletes fortune denoted at index 2
// Update a fortune:
$fortune[2] = "I never liked this fortune"; // update fortune at index 2
?>
Note: this will raise exceptions if a directory or multiple files have been set.
Adding fortunes
<?php
require_once 'File/Fortune.php';
$fortunes = new File_Fortune('/path/to/fortune/file');
$fortunes->add('Shiny, new fortune!');
?>
Note: this will raise exceptions if a directory or multiple files have been set.
Creating a new fortune file
<?php
require_once 'File/Fortune.php';
$newFortunes = array(
'Fortune 1',
'Fortune 2',
'Fortune 3'
);
$fortunes = new File_Fortune('/path/to/fortune/file');
$fortunes->create($newFortunes);
?>
Note: this will raise exceptions if a directory or multiple files have been set.