我是PHP OOP的新手。該項目需要一個站點級及以下是我的代碼():PHP OOP問題
class Sites {
private $siteName;
private $location;
private $postcode;
function __construct($name, $loc, $pc) {
$this->siteName = $name;
$this->location= $loc;
$this->postcode = $pc;
//use "insert SQL" to store new added site info to DB
$insertSQL = "INSERT INTO table_name (siteName, location, postcode) VALUES ($siteName, $location, $postcode)";
}
function getSiteName($SiteID){
selectSiteName = "SELECT siteName FROM site WHERE siteID = $SiteID";
return $this->siteName;
}
function getSiteLocation($SiteID){
selectLocation = "SELECT location FROM site WHERE siteID = $SiteID";
return $this->location;
}
function getPostCode($SiteID){
selectPostcode = "SELECT postcode FROM site WHERE siteID = $SiteID";
return $this->postcode;
}
function getSiteID(){
//what shoud write here?
return $this->siteID;
}
}
場在站點表包括「網站名稱」,「位置」,「郵政編碼」和「的siteID」。這裏'siteID'是主鍵和AUTO INCREMENT值。
我有幾個問題:
是上面的代碼是否正確?
如何獲得'SiteID'?例如,sitename是'ABC',應該使用「SELECT * FROM site WHERE siteName ='ABC'」來獲取ID。但網站名稱並不是唯一的價值。
對於像 'DeleteSiteByID' 方法, 'EditSiteByID', 'hasSubSites',shold那些方法是在站點類?
感謝您的幫助。
首先閱讀一些PHP OOP教程,因爲在你的類定義中有一些語法錯誤。 [This](http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect)和[this](http://www.massassi.com/php/articles/classes /)來自google上的第一個結果。你不能'getSiteName(int SiteID)'它應該是'getSiteName($ SiteID)'。 – Shef