php使用反射插入对象示例_PHP教程

WBOY
Freigeben: 2016-07-13 10:36:07
Original
634 Leute haben es durchsucht

 这篇文章主要介绍了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__)).'\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();         }     }  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/740411.htmlTechArticle这篇文章主要介绍了php使用反射插入对象示例,需要的朋友可以参考下 代码如下:/** * 插入insertModel(),利用反射,效率稍差 * @param class $mod...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!