锁定老帖子 主题:感觉PHP的最佳实践就是“数组编程”
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-07
PHP给我的感觉就是 小巧灵活
|
|
返回顶楼 | |
发表时间:2008-08-09
任何语言就语法层面来说,各有千秋
编程,光掌握语法是不行的 |
|
返回顶楼 | |
发表时间:2008-08-09
ctrlming 写道 PHP给我的感觉就是 小巧灵活
此豹之一斑耳 |
|
返回顶楼 | |
发表时间:2008-09-02
基于数组编程:),以前隐约有过这种思考,今天看到你的文章产生了共鸣了!
确实我试过mysqli_result的三个函数: fetch_assoc() fetch_array() fetch_row() assoc()居然占用内存最少,谁速度最快被我忘了..assoc()返回的可是map啊。 |
|
返回顶楼 | |
发表时间:2008-09-04
xombat 写道 基于数组编程:),以前隐约有过这种思考,今天看到你的文章产生了共鸣了! 确实我试过mysqli_result的三个函数: fetch_assoc() fetch_array() fetch_row() assoc()居然占用内存最少,谁速度最快被我忘了..assoc()返回的可是map啊。 我估计最快的应该是 fetch_row(),因为返回数组的key就是顺序数字。 主要是呀 fetch_array() 生成的数据是 fetch_assoc() + fetch_row() 合并而成的,所以效率不怎么样,不过它是为简化开发做的。 |
|
返回顶楼 | |
发表时间: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) { } } 对我来说,很多情况下已经够用了。 |
|
返回顶楼 | |