看板 PHP 關於我們 聯絡資訊
※ 引述《JYHuang (夏天到了,冷不起來了說)》之銘言: 請把 class DB 改成這樣 (我不確定你是不是用 PHP5 ,所以直接用 var): class DB { var $_result = NULL; function connect($server) { mysql_connect(......); } function query($sql) { $this->_result = mysql_query($sql); } function fetch() { return mysql_fetch_array($this->_result); } } : $databse = DB::connect($server); : $result = $databse->query($sql); : $row = $result->fetch(); <--問題出在這裡 你的 $result 已經傳出來到物件外面了。 $this 指的是物件本身,而你原本定義的這個物件裡面根本沒資料, 只有三個 method : connect、query、fetch 。 所以,在物件裡面的 fetch() 呼叫 mysql_fetch_array($this) 時, 傳遞的參數錯得很離譜,因為 $this 是物件本身。 而且,你也沒有在物件裡面定義變數來存 query result 。 : 我的理解是 : $result 已經不是跟$databse是同一個DB建構出來的實體了 : 所以$result不能用DB的子建構… : 是這樣嗎?那又要如何去.."繼承(要這樣講嗎?)"呢? 物件的繼承不是這樣解釋的,建議您多看些資料或書籍。 如果要學物件導向,我會建議用 JAVA 。 如果您熟悉 PHP ,不想碰 JAVA ,網頁上也有些 sample 。 PHP4 可以參考 http://tw.php.net/manual/en/language.oop.php 。 PHP5 可以參考 http://tw.php.net/manual/en/language.oop5.php 。 當然,有看過的人都知道, PHP 5 才真的有物件導向的精神。 所以,我會建議您用 PHP 5 , PHP 4 並不適合拿來學習物件導向。 -- Linux is for people who want to know why it works. Mac is for people who don't want to know why it works. DOS is for people who want to know why it does not work. Windows is for people who don't want to know why it does not work. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.113.173.172 ※ 編輯: JoeHorn 來自: 59.113.173.172 (01/16 00:19)
JYHuang:十分感謝~ 01/16 00:33