PHP Using Reflection Insert Object Example _php Tutorial

Source: Internet
Author: User
Tags rtrim
This article mainly introduces PHP using reflection Insert Object example, the need for friends can refer to the following

The code is as follows:/** * Insert Insertmodel (), use reflection, less efficient * @param class $model Object * @param bool $is _returnlastinsertid returns the Add ID * @return int The default return succeeds or not, $is _returnlastinsertid to True, returns the Add 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 ();}}

http://www.bkjia.com/PHPjc/740411.html www.bkjia.com true http://www.bkjia.com/PHPjc/740411.html techarticle This article mainly introduces PHP using reflection Insert Object example, the need for friends can refer to the following code:/** * Insert Insertmodel (), using reflection, less efficient * @param class $mod ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.