2011-09-15 38 views
1

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

我有幾個問題:

  1. 是上面的代碼是否正確?

  2. 如何獲得'SiteID'?例如,sitename是'ABC',應該使用「SELECT * FROM site WHERE siteName ='ABC'」來獲取ID。但網站名稱並不是唯一的價值。

  3. 對於像 'DeleteSiteByID' 方法, 'EditSiteByID', 'hasSubSites',shold那些方法是在站點類?

感謝您的幫助。

+0

首先閱讀一些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

回答

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

對於最後一個問題,這取決於你。但我更喜歡他們也是類方法。

0
  1. PHP解釋器會告訴你:)
  2. 這不是你的對象,但你的數據庫結構。如果您的網站名稱不唯一,則應在構建對象時傳遞一個ID。
  3. 他們可以。

您的對象似乎類似於活動記錄模式。看看這個:http://en.wikipedia.org/wiki/Active_record_pattern

0

1您應該添加知名度,你的方法,如:

public function __construct(){} 

您應該指定構造函數的參數先前聲明如下成員:

public function __ construct($siteName, $location, $postcode){ 
    $this->siteName= $siteName; 
    $this->location= $location; 
    $this->postcode= $postcode; 
} 

2- Im'not一定要理解你在這裏的意思,但是id應該是你的類的一個成員,當一個新對象被創建時它就會被填充。

3我不這麼認爲,這種方法應該在某些數據庫適配器中,例如,可以爲數據庫上的特定查詢返回一個Sites對象。

+3

根據手冊 - _Methods宣佈沒有任何明確的可見性關鍵字被定義爲public._ –

+2

你是對的,但我認爲明確設置可見性是一個很好的做法。順便說一句,我不知道一個好的開發人員與隱性知名度 – grunk

+0

我正在節省字節=) –

0

我來回答你的問題popint明智的。

  1. 您的代碼不正確。您傳遞給函數的參數無法被php識別。你應該試試

    function getSiteName($SiteId){ 
        //statements 
        } 
    
  2. 你的第二個問題是有點混淆。它似乎更像是一個與數據庫相關的問題。要獲得ID字段,最好的方法是通過對象返回它。而不是試圖從其他字段中查找ID,您應該嘗試從ID中獲取其他字段。良好的做法)

  3. 是的,您規定的方法應該留在網站類,因爲他們處理的網站表中的數據。

如果您能改善您的問題並詳細解釋您的問題,可以幫助您更好地完成工作。

+0

謝謝@Radheshyam納亞克! 'siteID'不能是構造中的參數,因爲siteID是TABLE中的AUTO INCREMENT FIELD。如果構造如:function __construct($ siteID,$ siteName,$ location,$ postcode)。當新的obj被實例化時,'siteID'可以是任何值。 – Acubi

0

一)我猜的構造方法應該是這樣的,因爲你聲明的其他名稱類的屬性...

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,例如一類名爲鏈接:

$site_links = $links->getSiteLinksBySiteId($foo_site->getId()); 

三)我將不包括deleteSiteById諸如此類在上課,我將它們包含在一個類,處理這些網站,通常是針對數據庫的模型。

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 - 這真的取決於你的數據庫設計(可以使站點名稱潮頭)

3-確定你可以在這個類中定義所有這些函數