我是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;
}
}
站点表中的字段包括“siteName”,“location”,“postcode”和“siteID”。这里'siteID'是主键和AUTO INCREMENT值。
我几乎没有问题:
上面的代码是否正确?
如何获取'SiteID'?例如,sitename是'ABC',应该使用“SELECT * FROM site WHERE siteName ='ABC'”来获取ID。但网站名称不是唯一值。
对于“ DeleteSiteByID ”,“ EditSiteByID ”,“ hasSubSites ”等方法,请将这些方法归入网站类
感谢您的帮助。
答案 0 :(得分:2)
构造函数错误。它应该是
function __construct($siteName, $location, $postcode) {
$this->siteName= $siteName;
$this->location= $location;
$this->postcode= $postcode;
}
因为它们是您在班级中声明的属性。
我建议你宣布另一个属性:public $id
是否在实例化后将Site
存储到数据库中?或者您有save()
方法吗?
如果您在实例化后立即存储它,那么mysql_insert_id()
将能够为您提供Site
的ID。
但如果没有,则使用站点名称和位置进行查询。我认为它的组合将足够独特。
如果确实声明了id
属性,则不需要方法的参数。您只需使用$this->id
。
对于最后一个问题,这取决于你。但我更希望他们也是阶级方法。
答案 1 :(得分:0)
您的对象似乎类似于Active Record Pattern。看看这个:http://en.wikipedia.org/wiki/Active_record_pattern
答案 2 :(得分:0)
1-您应该为方法添加可见性,例如:
public function __construct(){}
您应该将构造函数参数分配给先前声明的成员,如:
public function __ construct($siteName, $location, $postcode){
$this->siteName= $siteName;
$this->location= $location;
$this->postcode= $postcode;
}
2-我不确定你在这里理解你的意思,但是id应该是你的类的成员,当创建一个新对象时它将被填充。
3-我不这么认为,这种方法应该在某个数据库适配器中,例如,它可以为数据库上的特定查询返回Sites对象。
答案 3 :(得分:0)
您也无法使用int
进行类型提示:"Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported."
答案 4 :(得分:0)
让我回答你的问题。
您的代码不正确。您传递给函数的参数无法被php识别。你应该尝试
function getSiteName($SiteId){
//statements
}
你的第二个问题有点令人困惑。它似乎更像是与数据库相关的问题。要获取ID字段,最好的方法是通过对象返回它。而不是尝试从其他字段中查找ID,您应该尝试从ID获取其他字段。(被认为是好的做法)
是的,您规定的方法应保留在网站类中,因为它们处理网站表中的数据。
如果您可以改进问题并详细解释您的问题,可以更好地帮助您。
答案 5 :(得分:0)
a)我猜构造函数方法应该是这样的,因为你用其他名称声明了类属性......
function __construct($siteName, $location, $postcode) {
$this->siteName = $siteName;
$this->location = $location;
$this->postcode = $postcode;
}
b)你应该在类中包含一个id,所以你也可以使用id构造它,然后在需要时检索id。例如:
$site_parameters = DB::query("SELECT id, sitename, location, postcode FROM sites WHERE sitename='foo' LIMIT 1");
extract($site_parameters);
$foo_site = new Site($sitename, $location, $postcode, $id);
如果实现方法Site :: getId();您可以检索任何其他需要id的方法的id,例如名为links的类:
$site_links = $links->getSiteLinksBySiteId($foo_site->getId());
c)我不会在该类中包含deleteSiteById等,我会将它们包含在处理站点的类中,通常是对数据库起作用的模型。
答案 6 :(得分:0)
1-在我看来,最好使用PHP魔术方法并以这种方式构建你的类:
<?php
class Sites {
private $siteName = "";
private $Location = "";
private $postCode = "";
private $siteID = "";
private $allSites;//if you want to return all sites info you should set this property
public function __construct()
{
$this->allSites = array();
}
public function __set($field,$value)
{
switch($field){
case "siteName":
$this->siteName= $value;//do not forget validation before set
break;
case "siteID":
$this->siteID= $value;//do not forget validation before set
break;
case "Location":
$this->Location = $value;//do not forget validation before set
break;
case "postCode":
$this->postCode= $value;//do not forget validation before set
break;
default :
die("Error : property does not exist");
break;
}
}
public function __get($field)
{
switch($field){
case "siteName":
return $this->siteName;
break;
case "siteID":
return $this->siteID;
break;
case "Location":
return $this->Location;
break;
case "postCode":
return $this->postCode;
break;
default :
die("Error : property does not exist");
break;
}
}
function addSite()
{
//use "insert SQL" to store new added site info to DB
$insertSQL = "INSERT INTO site
(siteName, location, postcode)
VALUES ('".$this->siteName."', '".$this->Location."', '".$this->postCode."')";
//after Exequte
if(!$rs)
{
return false;
die();
}
$this->siteID = $rs->insertID;//it depends on your ORM or db connection
return true;
}
function getSiteName(){
$selectSiteName = "SELECT siteName FROM site WHERE siteID =". $this->SiteID;
//after Exequte
if(!$rs)
{
return false;
die();
}
$this->siteName = $rs->field['siteName'];//it depends on your ORM or db connection
return true;
}
function getSiteLocation($SiteID){
selectLocation = "SELECT location FROM site WHERE siteID = ". $this->SiteID;
//after Exequte
if(!$rs)
{
return false;
die();
}
$this->Locattion= $rs->field['location'];//it depends on your ORM or db connection
return true;
}
function getPostCode($SiteID){
selectPostcode = "SELECT postcode FROM site WHERE siteID = ". $this->SiteID;
//after Exequte
if(!$rs)
{
return false;
die();
}
$this->postCode= $rs->field['postcode '];//it depends on your ORM or db connection
return true;
}
}
$obj = new Sites();
$obj->siteName = $_POTS['siteName'];//also you can validate your data here before send it to class
$obj->Location = $_POTS['Location '];//also you can validate your data here before send it to class
$obj->postCode = $_POTS['postCode '];//also you can validate your data here before send it to class
$rs = $obj->addSite();
if($rs)
{
echo "Siet Name : ".$obj->siteName."<br>";
echo "Location : ".$obj->Location."<br>";
echo "Post Code : ".$obj->postCode."<br>";
}
else
{
echo "Error : site info was not added";
}
?>
2-这实际上取决于您的数据库设计(您可以将siteName设为uniqe)
3-确保您可以在此课程中定义所有这些功能