我剛剛開始使用OOP,所以這些都是我第一次使用babysteps!一個類的實例是否可以在另一個類的實例中更改變量?
我想建立一個簡單的預訂系統,用戶可以預訂其他用戶通過日曆創建的時間段。我處於項目的早期階段,因此數據庫尚未建立,我目前正在擺弄一天課和一個時隙課,並試圖弄清楚如何使它們互動。
這裏是我的代碼:
class dayCL {
public $dayInMonth;
public $numBookingSlotsOnDay;
public function __construct($dayInMonth) {
$this->dayInMonth = $dayInMonth;
}
public function getNumBookingSlotsOnDay(){
return "Booking slots: " . $this->numBookingSlotsOnDay;
}
public function numBookingSlotsOnDay(){
$this->numBookingSlotsOnDay = $this->numBookingSlotsOnDay + 1 ;
}
};
class bookingSlotCL {
public $startTime;
public function __construct($startTime) {
$this->startTime = $startTime;
}
public function output(){
return "Start: " . $this->startTime;
}
};
的numBookingSlotsOnDay
功能按預期工作調用時,但有可能以整合其在bookingSlotCL
通話,所以它被稱爲一個bookingSlotCL
的每一個實例?
實施例:
$day1 = new dayCL(1);
$bookingSlot1 = new bookingSlotCL(1);
$bookingSlot2 = new bookingSlotCL(1);
兩個預訂槽被放置在$day1
所以$day1->getNumBookingSlotsOnDay
應該返回「2」。
日曆的設計使所有的日子都被實例化爲dayCL
實例,這意味着一個bookingSlotCL
實例將始終有一個要連接的實例dayCL
。
回聲$ day1->天[0 ] - >輸出();作品也是!這是我正在尋找的解決方案!真棒!謝謝! :) –