PHP oop之大讨论
由于是午休时间写的 所以不太详细尽情谅解。主要还是老生常谈的问题 OOP 。。。
工作两年 对OOP 这玩意有些自己的理念 ,但翻遍了各大书店的OOP 介绍都是天马行空 (很像是一个人写的)
<?php
class Car{
public getName(){
return $this->name;
}
public getSpeed(){
return $this->speed;
}
}
?>
这种东西相信只会出现在书上 真正项目基本看不到这类代码。。(JAVAbean除外)
而根据这类思路我有了以下代码:
<?php
class 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>';
}
?>
但这却和项目开发中的代码有悖
<?php
class Order{
public function __construct(){}
public function getGoodsInfo(array('name','price')){
//进行数据结构的组织
}
}
$order2 = new Order('AANBA02');
echo $order2->getGoodsInfo(array('price'));
?>
个人觉得第一种比较符合面向对象规范 。但实际开发中往往都是第二种。
因为不是一个人开发所以只能顺着大部分程序员的写法来。 但这样代码的可复用性 大打折扣
相同功能写上好几遍不知大家是否支持我的看法。 欢迎拍砖~
页:
[1]