使いどころ 既存のクラスに変更を加えたいが、直接ソースの変更を行いたくない。 WatchYoukai.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php /** * 指定されたファイルを表示するクラスです */ class WatchYoukai { private $filename; public function __construct($filename_) { if( !is_readable($filename_) ) { throw new Exception("エラー:{$filename_}は開くことが出来ません。"); } $this->filename = $filename_; } /** * ファイルを表示させます */ public function showPlain() { echo "<pre>"; echo htmlspecialchars(file_get_contents($this->filename), ENT_QUOTES, mb_internal_encoding()); echo "</pre>"; } /** * ファイルをハイライトで表示させます */ public function showHighLight() { highlight_file($this->filename); } } |
client.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php require_once 'WatchYoukai.class.php'; try{ $youkaiObj = new WatchYoukai('./WatchYoukai.class.php'); }catch(Excetion $e){ die($e->getMessage()); } $youkaiObj->showPlain(); echo "<hr/>"; $youkaiObj->showHighLight(); |
…