Introduction (Previous) (Next) File_Fortune::__construct

View this page in Last updated: Sun, 28 Sep 2008
English | French | German | Japanese | Plain HTML

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.

Introduction (Previous) (Next) File_Fortune::__construct

Download Documentation Last updated: Sun, 28 Sep 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
There are no user contributed notes for this page.