導入 (Previous) (Next) File_Fortune::__construct

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

例 --  よくある使用法

ランダムな fortune の取得


<?php
require_once 'File/Fortune.php';

// 単一の fortune ファイルから取得します
$fortunes = new File_Fortune('/path/to/fortune/file');
echo $fortunes->getRandom();

// 複数の fortune ファイルを含むディレクトリから取得します
$fortunes = new File_Fortune('/path/to/fortune/files/');
echo $fortunes->getRandom();
?>

すべての fortunes の取得


<?php
require_once 'File/Fortune.php';

$fortunes = new File_Fortune('/path/to/fortune/file');
$cached $fortunes->getAll();

// あるいは、すべてのファイルからすべての fortune を取得します
$fortunes->setDirectory('/path/to/fortunes/');
$cached $fortunes->getAll();
?>

fortune の数の取得


<?php
require_once 'File/Fortune.php';

$fortunes = new File_Fortune('/path/to/fortune/file');
$count count($fortunes);

// あるいは、すべてのファイルに含まれる fortune の数を調べます
$fortunes->setDirectory('/path/to/fortunes/');
$count count($fortunes);
?>

fortune のループ処理


<?php
require_once 'File/Fortune.php';

$fortunes = new File_Fortune('/path/to/fortune/file');
foreach ($fortunes as $fortune) {
    echo $fortune;
}
?>

注意: これは、複数のファイルやディレクトリを指定した場合には例外を発生します。

fortune の操作


<?php
require_once 'File/Fortune.php';

$fortunes = new File_Fortune('/path/to/fortune/file');

// fortune の削除
unset($fortunes[2]); // 2 番目の位置にある fortune を削除します

// fortune の更新
$fortune[2] = "こんな占いなんて大っ嫌い"// 2 番目の位置にある fortune を更新します
?>

注意: これは、複数のファイルやディレクトリを指定した場合には例外を発生します。

fortune の追加


<?php
require_once 'File/Fortune.php';

$fortunes = new File_Fortune('/path/to/fortune/file');
$fortunes->add('光り輝く新規メッセージ!');
?>

注意: これは、複数のファイルやディレクトリを指定した場合には例外を発生します。

新しい fortune ファイルの作成


<?php
require_once 'File/Fortune.php';

$newFortunes = array(
    'メッセージ 1',
    'メッセージ 2',
    'メッセージ 3'
);

$fortunes = new File_Fortune('/path/to/fortune/file');
$fortunes->create($newFortunes);
?>

注意: これは、複数のファイルやディレクトリを指定した場合には例外を発生します。

導入 (Previous) (Next) File_Fortune::__construct

Download Documentation Last updated: Sun, 31 Aug 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.