2012-02-02 54 views
2

這不是一個真正的問題,而是更多的問題。 我的問題是它是如何被「編程」的。 雖然可能不太清楚。PHP OOP/MVC方法

所以我的問題是,我必須使多種方法從數據庫中檢索數據嗎?例如,我有一個名爲FRUITS的表。 它包含添加水果的ID,名稱和日期。 現在我想根據給定的ID獲取水果的名稱,稍後在腳本中我也想獲得水果的日期。

我應該做一個方法,比如get_fruit($ id),它返回名稱和日期,還是兩個單獨的方法get_name($ id)和get_date($ id)?

在此先感謝。

回答

3

您應該使用一個包含所有必需數據的對象。例如:

class Fruit { 

    protected ... variables; 

    public function getId() {...} 
    public function getDate() {...} 
    ... 

} 

也在實施__set__get將使用完整的PHP潛在的很好的例子。

您也可以實現save()方法(或擴展數據庫的行類,如Zend_Db_Table_Row

所以整個代碼看起來:

$fruit = $model->getFruid(7); // $id = 7 :) 
echo $fruit->id; // would call internally $fruit->__get('id') 
echo $fruit->date; 

// And modification: 
$fruit->data = '2011-08-07'; 
$fruit->save(); 

編輯:使用不同的方法來加載某些數據當您需要加載大量數據(例如長文本)時,它是有用的(僅限於?),這些數據僅在代碼中的一個位置上需要,並且會影響性​​能。

編輯2:(回答評論): __get__set當您嘗試訪問對象的未定義的屬性被稱爲,例如:

class Foo { 
    public $bar; 
    public function __get($name){ 
     echo $name "\n"; 
     return 'This value was loaded'; 
    } 
} 

// Try to use 
Foo $foo; 
echo $foo->bar . "\n"; 
echo $foo->foo . "\n"; 

有兩個「大」接近這一點,我瞭解:

// First use __get and __set to access internal array containing data 
class DbObject { 
protected $data = array(); 

public function __get($propertyName){ 
    // Cannot use isset because of null values 
    if(!array_key_exits($propertyName,$this->data)){ 
     throw new Exception('Undefined key....'); 
    } 

    return $this->data[ $propertyName]; 
} 

// Don't forget to implement __set, __isset 
} 

// Second try to call getters and setter, such as: 
class DbObject { 
public function getId() { 
    return $this->id; 
} 

public function __get($propertyName){ 
    $methodName = 'get' . ucfirst($propertyName); 
    if(!method_exits(array($this, $methodName)){ 
     throw new Exception('Undefined key....'); 
    } 
    return $this->$methodName(); 
} 
} 

總結起來......第一種方法是容易實現的,是完全自動化的......你並不需要大量的代碼和來源將是幾乎每個類別的相同。第二種方法需要更多的編碼,但給你一個更好的控制。例如:

public function setDate($date){ 
    $this->date = date('Y-m-d h:i:s', strtotime($date)); 
} 

但在另一方面,你可以用第一種方法做到這一點:

class Fruit extends DbObject { 
public function __set($key, $val){ 
    switch($key){ 
    case 'date': 
     return $this->setDate($val); 

    default: 
     return parent::__set($key, $val); 
    } 
} 
} 

或者你可以用總組合併爲您的getter/setter第一,比嘗試訪問屬性直接...

+0

明確的解釋。謝謝。 __set和__get應該用於什麼?我讀了無法訪問的對象(so..Private對象?) – Roel 2012-02-02 10:23:51

+0

@Roel我已經編輯了答案,你現在能理解這一點嗎? – Vyktor 2012-02-02 10:46:31

0

這裏是代碼如何使用一個函數來獲得不同的字段的值。

function get_fruit($id,$field = ''){ 

    $sql = "select * from table_name where id = $id"; 
    $result = mysql_fetch_object(mysql_query($sql)); 

    if($field != ''){ 
    return $result->$field; 
    }else{ 
    return $result; 
    } 
} 

echo get_fruit(1,'field_name');

0
class data_retrieve 
{ 
    public $tablename; 
    public $dbname; 
    public $fieldset; 
    public $data_array; 
    public $num_rows 
    function __construct() 
    { 
     $this->tablename='junk'; 
     $this->dbname='test'; 
     $this->fieldset=array('junk_id'); 

    } 
function getData($where_str) 
    { 
     $this->data_array= array(); 

        global $dbconnect, $query; 
     if($dbconnect ==0) 
     { 
      echo" Already Connected \n "; 
      $dbconnect=db_connect("objectdb") or die("cannot connect"); 
     } 

     $where_str; 

     if(empty($where_str)) 
     { 
      $where_str=NULL; 
     } 
     else 
     { 
      $where_str= "where". $where_str ; 
     } 

$query= "select * from $this->tablename $where_str"; 
$record= mysql_query($query) or die($query); 
     $recNo=mysql_num_rows($record); 

     $this->num_rows=$recNo; 
        while($row= mysql_fetch_assoc($record)) 
     { 
      $this->data_array[]=$row; 
     } 

     mysql_free_result($record); 

     return $this->data_array; 
} 

class fruit extends data_retrieve 
{ 

function __construct() 
    { 
     parent::__construct(); 
     $this->tablename='fruit'; 
     $this->fieldset=array('fruit_id','fruit_name','date'); 

    } 
} 

然後 在文件中創建一個水果的物體,像

$str="fruit_id=5"; 
$fruit_data = new fruit(); 

$records=$fruit_data->getData($str); 

顯示

foreach($records as $row) 
{ 
print <<< HERE 
<label class='table_content' > $row[fruit_id]</label> 
<label class='table_content' > $row[fruit_name]</label> 
<label class='table_content' > $row[date]</label> 
HERE; 
}