您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在php中使用反射插入对象

2025/2/4 10:16:31发布30次查看
在php中使用反射插入对象
/** * 插入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__)).'modelsbasemodel.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(); } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product