|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
通过这段时间的学习实践,对软件开发有了更多新的认识,不在局限于之前的片面性。当然,现在所学到的东西其实并不多,离当一个真正的程序员,还有很大的差距。 当一个对象包括庞杂独自立的,必需基于判决履行的功效性的若干局部时,最好的办法是合用基于拜托设计形式的对象。
- <?php /** * 示例: Web站点具有创立
MP3文件播放列表的功效
, 也具有选择以 M3U 或 PLS 格局
下载播放列表的功效
。 * 以下代码示例展现
惯例
与拜托
两种形式
完成
*/ //惯例
完成
class Playlist { private $_songs; public function __construct() { $this->_songs = array(); } public function addSong($location, $title) { $song = array("location" => $location, "title" => $title); $this->_songs[] = $song; } public function getM3U() { $m3u = "#EXTM3U\n\n"; foreach ($this->_songs as $song) { $m3u .= "#EXTINF: -1, {$song['title']}\n"; $m3u .= "{$song['location']}\n"; } return $m3u; } public function getPLS() { $pls = "[playlist]]\nNumberOfEntries = ". count($this->_songs) . "\n\n"; foreach ($this->_songs as $songCount => $song) { $counter = $songCount + 1; $pls .= "File{$counter} = {$song['location']}\n"; $pls .= "Title{$counter} = {$song['title']}\n"; $pls .= "LengthP{$counter} = -1 \n\n"; } return $pls; } } $playlist = new Playlist(); $playlist->addSong("/home/aaron/music/brr.mp3", "Brr"); $playlist->addSong("/home/aaron/music/goodbye.mp3", "Goodbye"); $externalRetrievedType = "pls"; if ($externalRetrievedType == "pls") { $playlistContent = $playlist->getPLS(); } else { $playlistContent = $playlist->getM3U(); } echo $playlistContent; //拜托
形式
完成
class newPlaylist { private $_songs; private $_tyepObject; public function __construct($type) { $this->_songs = array(); $object = "{$type}Playlist"; $this->_tyepObject = new $object; } public function addSong($location, $title) { $song = array("location" => $location, "title" => $title); $this->_songs[] = $song; } public function getPlaylist() { $playlist = $this->_tyepObject->getPlaylist($this->_songs); return $playlist; } } class m3uPlaylist { public function getPlaylist($songs) { $m3u = "#EXTM3U\n\n"; foreach ($songs as $song) { $m3u .= "#EXTINF: -1, {$song['title']}\n"; $m3u .= "{$song['location']}\n"; } return $m3u; } } class plsPlaylist { public function getPlaylist($songs) { $pls = "[playlist]]\nNumberOfEntries = ". count($songs) . "\n\n"; foreach ($songs as $songCount => $song) { $counter = $songCount + 1; $pls .= "File{$counter} = {$song['location']}\n"; $pls .= "Title{$counter} = {$song['title']}\n"; $pls .= "LengthP{$counter} = -1 \n\n"; } return $pls; } } $externalRetrievedType = "pls"; $playlist = new newPlaylist($externalRetrievedType); $playlist->addSong("/home/aaron/music/brr.mp3", "Brr"); $playlist->addSong("/home/aaron/music/goodbye.mp3", "Goodbye"); $playlistContent = $playlist->getPlaylist(); echo $playlistContent; ?>
复制代码 数据库剧本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html
学会了PHP,那么学其他的语言,肯定速成,反过来也一样,如果你之前学过其他的语言,那么学PHP肯定快。 |
|