PHP oop之大討論 — 你究竟有無在用OOP

來源:互聯網
上載者:User

   由於是午休時間寫的 所以不太詳細盡情諒解。

   主要還是老生常談的問題 OOP 。。。

   工作兩年 對OOP 這玩意有些自己的理念 ,但翻遍了各大書店的OOP 介紹都是天馬行空 (很像是一個人寫的)

   

<?php  class Car{            public getName(){          return $this->name;      }      public getSpeed(){          return $this->speed;      } }?>

這種東西相信只會出現在書上 真正項目基本看不到這類代碼。。(JAVAbean除外)

而根據這類思路我有了以下代碼:

<?phpclass Order{     private _GoodsArr = array();     public function __construct(){             }     /*      * @return Goods {}      */     public function getGoods(){        //尋找訂單下的商品編號  Goods_sn        foreach($Goods_sn as $goods_sn)           array_push($this->_GoodsArr, new Goods($goods_sn));        return $this->_GoodsArr;     }}class Goods{    public function getName(){}}$order1 = new Order('AANBA01');$goodsArr = $order1->getGoods();foreach($goodsArr as $goods){   echo $goods->getName().'<br>';}?>

但這卻和項目開發中的代碼有悖

<?phpclass Order{        public function __construct(){}    public function getGoodsInfo(array('name','price')){       //進行資料結構的組織       }}$order2 = new Order('AANBA02');echo $order2->getGoodsInfo(array('price'));?>

個人覺得第一種比較符合物件導向規範 。但實際開發中往往都是第二種。

因為不是一個人開發所以只能順著大部分程式員的寫法來。 但這樣代碼的可複用性 大打折扣 

相同功能寫上好幾遍  不知大家是否支援我的看法。 歡迎拍磚~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.