源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php使用反射插入对象示例分享

  • 时间:2020-11-26 20:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php使用反射插入对象示例分享
[u]复制代码[/u] 代码如下:
/**      * 插入insertModel(),利用反射,效率稍差     * @param class $model 对象     * @param bool $is_returnLastInsertId 是否返回添加ID     * @return int 默认返回成功与否,$is_returnLastInsertId 为true,返回添加ID     */     public function insertModel($model,$is_returnLastInsertId=FALSE) {         try {             require_once dirname(dirname(__FILE__)).'\Models\BaseModel.php';             if(!is_subclass_of($model, "BaseModel")){                 exit($this->getError(__FUNCTION__, __LINE__));             }             $className=get_class($model);             $tName = $this->formatTabName($className);             $reflectionClass=new ReflectionClass($className);             $properties=$reflectionClass->getProperties();             unset($properties[0]);             $fields="";             $vals="";             foreach ($properties as $property) {                 $pName=$property->getName();                 $fields.=$pName.",";                 $vals.='\''.$model->$pName.'\''.',';             }             $fields=rtrim($fields,',');             $vals=rtrim($vals,',');             $this->sql = "insert into {$tName} ({$fields}) values ({$vals})";             if($is_returnLastInsertId){                 $this->conn->exec($this->sql);                 $lastId = (int)$this->conn->lastInsertId();                 return $lastId;             }  else {                 $row = $this->conn->exec($this->sql);                 return $row;             }         } catch (Exception $exc) {             echo $exc->getMessage();         }     }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部