`
yipsilon
  • 浏览: 242678 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

感觉PHP的最佳实践就是“数组编程”

阅读更多

也研究了一段时间的PHP,使用过了一堆libraries,之后发现:原来PHP的最佳实践就是“数组编程”啊!

从以下几个角度来看:

1. 基本语法(foreach等、array函数库):对于数组的操作是最简便的,而且还支持literal样式的数组(类似于Java的Map),不用耗费多少时间就可以掌握。

2. 核心引擎(Zend Engine):面向对象的编程开发量比较繁琐,没有Java那么方便。更重要的是,PHP引擎处理对象的效率比较低。

3. 数据库访问(PHP原生):返回出来数组时效率最高,而且还最简单。

4. 模板引擎(Smarty等):使用数组时,对模板制作最方便(直接使用“.”就可以了)。

个人感觉,以后要做PHP项目时,首推的就是面向数组的编程,既快速又高效....

分享到:
评论
15 楼 showerxpII 2008-09-08  
class TableManager {
		private $tableName,$fields,$conn;
		
		public function __construct($con){
			$this->fields = array();
			$this->conn=$con;
		}
		public function __destruct(){
			
		}
		public function getConn(){
			return $this->conn;
		}
		public function getTableName(){
			return ($this->tableName);
		}
		public function setTableName($value){
			$this->tableName=$value;
		}
		public function getFields(){
			return ($this->fields);
		}
		public function setFields($value){
			$this->fields=$value;
		}
		//添加新的数据库记录
		public function saveNew()
		{
			//一个空的纪录集合 
			$rs=$this->conn->Execute("SELECT * FROM ".$this->tableName);
			$insertSQL = $this->conn->GetInsertSQL($rs, $this->fields);
			$this->conn->Execute($insertSQL); 
		}
		//修改数据库记录
		public function update($fieldName,$fieldValue)
		{
			$rs=$this->conn->Execute("SELECT * FROM ".$this->tableName." where ".$fieldName."=".$fieldValue);
			$updateSQL = $this->conn->GetUpdateSQL($rs, $this->fields);
			$this->conn->Execute($updateSQL); # 更新资料库中的记录
		}
		public function updateRecords($sqlWhere){
			$rs=$this->conn->Execute("SELECT * FROM ".$this->tableName." where ".$sqlWhere);
			$updateSQL = $this->conn->GetUpdateSQL($rs, $this->fields);
			$this->conn->Execute($updateSQL); # 更新资料库中的记录
		}
		public function delete(){}
	}	
class PicTableManager extends TableManager {
		private $uploadFiles;//数组,保存图片路径字段.
					//数组结构:$uploadFiles["uploadFieldName"]=uploadFileObj;
					//eg,这里的pic是数据库中的字段名称:
					  // $objFile=new UploadFiler($_FILES['pic'],_FileDir);
					//   $uploadFiles['pic']=$objFile;	
		public function __construct($con){
			parent::__construct($con);
			$this->uploadFiles=array();
		}
		public function __destruct(){}
		public function setUploadFiles($uploadFilesV){
			$this->uploadFiles=$uploadFilesV;
		}
		public function getUploadFilesV(){
			return $this->uploadFilesV;
		}
		//上传图片方法
		private  function uploadFiles()
		{
			foreach ($this->uploadFiles as $uploadObj) {
				$uploadObj->uploadFile();
			}
		}
		//删除上传的图片,修改过的图片
		public function delUploadedFile($fieldName,$fieldValue)
		{
			$conn=$this->getConn();
			$sqlString="SELECT * FROM ".$this->getTableName()." where ".$fieldName."=".$fieldValue;
			$rs=$conn->Execute($sqlString);
			foreach ($this->uploadFiles as $uploadFieldName=>$uploadFileObj) {				
				if (file_exists(_FileDir.$rs->fields[$uploadFieldName])) {
    				unlink(_FileDir.$rs->fields[$uploadFieldName]);//删除文件   		
				}
				else continue;
			}
		}
		//覆写保存新纪录的方法
		public function saveNew()
		{
			$this->uploadFiles();
			$tempFields=$this->getFields();
			//生成用于保存的fields数组
			foreach ($this->uploadFiles as $uploadFieldName=>$uploadFileObj){
				$tempUploadfile=$uploadFileObj->getUploadFile();
				$tempFields[$uploadFieldName]=$tempUploadfile["name"];	//
			}
			$this->setFields($tempFields);
			parent::saveNew();
		}
		//覆写修改纪录的方法
		public function update($fieldName,$fieldValue)
		{
			$this->delUploadedFile($fieldName,$fieldValue);//删除对应纪录的有关修改过的老图片
			$this->uploadFiles();
			$tempFields=$this->getFields();
			foreach ($this->uploadFiles as $uploadFileTableName=>$uploadFileObj){				
				if (!$uploadFileObj->getUploadError()) {
					$fileTemp=$uploadFileObj->getUploadFile();
					$tempFields[$uploadFileTableName]=$fileTemp["name"];
				}
				else echo $uploadFileObj["error"];
			}
			$this->setFields($tempFields);
			parent::update($fieldName,$fieldValue);
		}
		public function delete($id)
		{
			
		}
   	}

对我来说,很多情况下已经够用了。
14 楼 yipsilon 2008-09-04  
xombat 写道
基于数组编程:),以前隐约有过这种思考,今天看到你的文章产生了共鸣了!

确实我试过mysqli_result的三个函数:
fetch_assoc()
fetch_array()
fetch_row()
assoc()居然占用内存最少,谁速度最快被我忘了..assoc()返回的可是map啊。

我估计最快的应该是 fetch_row(),因为返回数组的key就是顺序数字。

主要是呀 fetch_array() 生成的数据是 fetch_assoc() + fetch_row() 合并而成的,所以效率不怎么样,不过它是为简化开发做的。
13 楼 xombat 2008-09-02  
基于数组编程:),以前隐约有过这种思考,今天看到你的文章产生了共鸣了!

确实我试过mysqli_result的三个函数:
fetch_assoc()
fetch_array()
fetch_row()
assoc()居然占用内存最少,谁速度最快被我忘了..assoc()返回的可是map啊。
12 楼 七月十五 2008-08-09  
ctrlming 写道
PHP给我的感觉就是 小巧灵活


此豹之一斑耳
11 楼 七月十五 2008-08-09  
任何语言就语法层面来说,各有千秋
编程,光掌握语法是不行的
10 楼 ctrlming 2008-03-07  
PHP给我的感觉就是 小巧灵活
9 楼 Jackphone 2007-12-25  
zwws 写道

一直习惯不了.NET 总觉得MS的东西都好变态.


可是你大多数时间是在使用MS的东西吧
8 楼 zwws 2007-12-23  
PHP 的数组很灵活 性能也不错
一直习惯不了.NET 总觉得MS的东西都好变态.
7 楼 yipsilon 2007-12-21  
计算机语言的“面向”和“基于”是从英文中的“Oriented”和“Based”翻译来的。而这两个词可以有多种翻译,作为程序员,不要在这种无谓的地方钻牛角尖,最好把它用在程序开发上,说不定还会给自己带来很大好处。:)
6 楼 diogin 2007-12-21  
什么是“面向”对象?为什么是“面向”?什么又是“基于”对象?这些名词很傻。依我看,不如统一叫作“对象化编程”,与“结构化编程”相对应。
5 楼 Jackphone 2007-12-21  
不错,我也有同感

总觉得PHP没有真正的做到面向对象
4 楼 115300111 2007-12-12  
太神奇了!

数组虽好 可不能面对喔~
3 楼 yipsilon 2007-11-27  
算不上新词啦。
2 楼 diogin 2007-11-26  
呵呵,“面向数组编程”,你得到了精髓 :p
1 楼 flynetcn 2007-11-26  
面向数组的编程?
楼主发明的新词?
php的数组是一大特色,它实际上是有序图。

相关推荐

Global site tag (gtag.js) - Google Analytics