2011-09-15 125 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-确定你可以在这个类中定义所有这些函数