2010-04-29 127 views
2

我剛開始學習PHP OOP,以前我一直在以程序方式進行PHP操作。我讀this article和我有幾個簡單的問題,PHP OOP概念(值對象/數據訪問對象)

  1. 如何通常被定義爲值對象的構造?作爲一個接受所有「數據成員」作爲參數或堅持默認構造函數並使用mutator/accessor方法設置/獲取數據成員的方法?

  2. 這實際上是開始做PHP OOP的推薦方式嗎?誠實地說,文章中解釋的概念對我來說有點混亂。

乾杯

+2

我從來沒有在PHP上下文中看到過這些建議。 PHP程序員往往非常青睞MVC。該頁面上的幾條建議值得商榷,並不代表流行觀點。 – 2010-04-29 14:04:54

+0

*(替代閱讀)* http://carsonified.com/blog/dev/getting-started-with-oop-php5/ – Gordon 2010-04-29 14:47:08

+0

DAO模式是一種可以處理MVC模式中M層的一部分的方法。 DAO不是MVC的替代品。 – 2010-06-18 17:09:37

回答

1
  1. 要建立充當中立的數據容器,我會用經典的構造__contruct()然後你可以使用魔法__get__set訪問屬性的對象。 另一種方式在通過谷歌搜索「依賴注入」注入具有例如,你可以找到更多這方面的信息DB行對象數據

  2. 你可以啓動它在通過一個主類最OOP語言,但對網絡的關注MVC被廣泛使用。

您是否有以前的OOP編程經驗?

2

對象必須始終處於有效的狀態。這意味着對象總是表現(方法)幷包含有意義的信息(成員變量)。構造函數的目的是創建處於有效狀態的對象。

考慮到值對象,構造函數必須採用最少數量的參數,以便該對象有意義。例如,如果你有一個RGB值對象,如果紅色是字符串「猴子」,綠色是NULL,藍色是數組,那麼在你的應用程序中是否有意義?

final class RGB { 
    $r, $g, $b; 
} 

$mycolor = new RGB; 

// NULL, what value of red is that supposed to be? 
var_dump($mycolor->r); 

// We are free to set these values too, even though they do not make sense. 
$mycolor->r = 'monkey'; 
$mycolor->g = NULL; 
$mycolor->b = array('foo', 'bar'); 

這正是這個對象允許發生的情況。 $ mycolor引用一個不處於有效狀態的對象。我們如何確保RGB對象始終具有三個值,即紅色,藍色和綠色,並且它們都是0到255之間的數字?

final class RGB { 

    private $r; 
    private $g; 
    private $b; 

    public function __construct($r, $g, $b) { 

     // are our arguments numbers? 

     if (!is_numeric($r)) { 
      throw new Exception('Value of red must be a number'); 
     } 
     if (!is_numeric($g)) { 
      throw new Exception('Value of green must be a number'); 
     } 
     if (!is_numeric($b)) { 
      throw new Exception('Value of blue must be a number'); 
     } 

     // are our arguments within range 0-255? 

     if ($r < 0 || $r > 255) { 
      throw new Exception('Value of red must be 0-255'); 
     } 
     if ($g < 0 || $g > 255) { 
      throw new Exception('Value of green must be 0-255'); 
     } 
     if ($b < 0 || $b > 255) { 
      throw new Exception('Value of blue must be 0-255'); 
     } 

     // our arguments are fine. 

     $this->r = $r; 
     $this->g = $g; 
     $this->b = $b; 

    } 

    //*// Canadian support 

    public function getColour() { 

     return $this->getColor(); 

    } 

    public function getColor() { 

     return array($this->r, $this->g, $this->b); 

    } 

} 

$mycolor = new RGB; // error! missing three arguments 
$mycolor->r = 'monkey'; // error! this field is private 

// exception! the constructor complains about our values not being numbers 
$mycolor2 = new RGB('monkey', NULL, array('foo', 'bar')); 

// exception! the constructor complains our numbers are not in range 
$mycolor3 = new RGB(300, 256, -10); 

$mycolor4 = new RGB(255, 0, 0); // ahh... a nice shade of red 
var_dump($mycolor4->getColor()); // we can read it out later when we need to 

如果你的構造函數需要三個參數,除非所有三個是數字0和255之間,你將永遠被定義與良好的價值觀成員變量,它拋出一個異常。

您還必須確保紅色,藍色和綠色成員變量是私人的。否則,任何人都可以寫任何他們想要的東西。當然,如果他們是私人的,任何人都無法閱讀。爲了創建一個只讀操作,我們定義了訪問私有成員變量的getColor()方法。

此版本的RGB對象將始終具有有效狀態。

希望這可以讓我們瞭解面向對象的本質並給你一個開始思考的地方。