首页 > 作文

使用PHP反射机制来构造”CREATE TABLE”的sql语句

更新时间:2023-04-07 11:32:55 阅读: 评论:0

反射是指在php运行状态中,扩展分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射api。反射是操纵面向对象范型中元模型的api,其功能十分强大,可帮助我们构建复杂,可扩展的应用。

其用途如:自动加载插件,自动生成文档,甚至可用来扩充php语言。

php反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互。借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传递参数,动态调用类的静态方法。

反射api是php内建的oop技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性,方法和扩展。这些oop扩展被称为反射。

下面的程序使用reflection来构造”create table”的sql语句。如果你不是很熟悉反射机制,可以从这个程序中看看反射的魅力与作用。

<?php/** * creates an sql 'create table' bad upon an entity * @author chris tankersley <chris@ctankersley.com> * @copyright 2010 chris tankersley * @package phporm_cli */class phporm_cli_generatesql{  /**   * u a mysql databa   */  const mysql = 'mysql';  /**   * u a sqlite databa   */  const sqlite = 'sqlite';  /**   * types that are allowed to have a length   * @var array   */  protected $_haslength = array('integer', 'varchar');  /**   * regexes needed to pull out the different comments   * @var array   */  protected $_regexes = array(    'type' => '/ type=([a-z_]*) /',    'length' => '/ length=([0-9]*) /',    'default' => '/ default="(.*)" /',    'null' => '/ null /',  );  /**   * types that we support   * @var array   */  protected $_validtypes = array(    'boolean' => 'bool',    'date' => 'date',    'integer' => 'int',    'primary_autoincrement' => 'int auto_increment primary key',    'text' => 'text',    'timestamp' => 'timestamp',    'varchar' => 'varchar',  );  /**   * name of the class we will interperet   * @var string   */  protected $_classname;  /**   * name of the table we are generating   * @var string   */  protected $_tablename;  /**   * the type of databa we are generating   * @var string   */  protected $_type;  /**   * ts the name of the class we are working with   * @param string $class   * @param string $table_name   * @param string $type   */  public function __construct($class, $table_name, $type = lf::mysql)  {    $this->_classname = $class;    $this->_tablename = $table_name;    $this->_type = $type;  }  /**   * builds an sql line for a property   * @param reflectionproperty $property   * @return string   */  protected function _getdefinition($property)  {    $type = '';    $length = '';    $null = '';    preg_ma怎样设置电脑自动关机tch($this->_regexes['type'], $property->getdoccomment(), $matches);    if(count($matches) == 2) {      if(array_key_exists($matches[1], $this->_validtypes)) {        $type = $this->_validtypes[$matches[1]]; lolcdk       if(in_array($matches[1], $this->_haslength)) {          $length = $this->_getlength圣埃克苏佩里($property);        }        if($matches[1] != 'primary_autoincrement') {          $null = $this->_getnull($property);   电子烟有害吗     }        $sql = '`'.$property->getname().'` '.$type.' '.$length.' '.$null;        return $sql;      } el {        throw new exception('type "'.$matches[1].'" is not a supported sql type');      }    } el {      throw new exception('found '.count($matches).' when checking type for property '.$property->getname());    }  }  /**   * extracts the length from a property   * @param reflectionproperty $property   * @return string   */  protected function _getlength($property)  {    preg_match($this->_regexes['length'], $property->getdoccomment(), $matches);    if(count($matches) == 2) {      return '('.$matches[1].')';    } el {      return '';    }  }  /**   * determines if a property is allowed to be null   * @param reflectionproperty $property   * @return string   */  protected function _getnull($property)  {    preg_match($this->_regexes['null'], $property->getdoccomment(), $matches);    if(count($matches) == 1) {      return 'null';    } el {      return 'not null';    }  }  /**   * generates a block of sql to create a table from an entity   * @return string   */  public function getsql()  {    $class = new reflectionclass($this->_classname);    $definitions = array();    foreach($class->getproperties() as $property) {      if(strpos($property->getname(), '_') === fal) {        $definitions[] = $this->_getdefinition($property);      }    }    $columns = implode(",n", $definitions);    $sql = "create table ".$this学校后勤管理制度->_tablename." (".$columns.")";    if($this->_type == lf::mysql) {      $sql .= " engine=myisam";    }    return $sql.";";  }}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对www.887551.com的支持。如果你想了解更多相关内容请查看下面相关链接

本文发布于:2023-04-07 11:32:53,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/85e909dbb0187a40f62dc025c0ab5c3b.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:使用PHP反射机制来构造”CREATE TABLE”的sql语句.doc

本文 PDF 下载地址:使用PHP反射机制来构造”CREATE TABLE”的sql语句.pdf

标签:反射   方法   程序   中元
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图