This article mainly introduces PHP using reflection Insert Object example, need friends can refer to the following
Code as follows:/** * Insert Insertmodel (), use reflection, slightly less efficiency * @param class $model object &nbs P * @param bool $is _returnlastinsertid whether to return add id * @return int succeeds or not by default, $is _returnlastinsertid is true, returns Tim Add id */ Public Function Insertmodel ($model, $is _returnlastinsertid=false) { try { require_once dirname (dirname (__file__)). ' Modelsbasemodel.php '; if (!is_subclass_of ($model, "Basemodel")) { &NB Sp 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) { & nbsp $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) { &NB SP $this->conn->exec ($this->sql); $lastId = (int) $this->conn->lastinsertid (); return $lastId; { else { $row = $t His->conn->exec ($this->sql); return $row; $EXC } catch (Exception) { &N Bsp echo $exc->getmessage (); } }