我是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