婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 使用PHP反射機(jī)制來(lái)構(gòu)造CREATE TABLE的sql語(yǔ)句

使用PHP反射機(jī)制來(lái)構(gòu)造CREATE TABLE的sql語(yǔ)句

熱門(mén)標(biāo)簽:保定crm外呼系統(tǒng)運(yùn)營(yíng)商 海南人工外呼系統(tǒng)有效果嗎 西區(qū)企業(yè)怎么做地圖標(biāo)注入駐 智能電話機(jī)器人排名前十名南京 阿里云400電話申請(qǐng)加工單 九江外呼系統(tǒng) 抖音有個(gè)地圖標(biāo)注是什么意思 地下城堡2圖九地圖標(biāo)注 七魚(yú)外呼系統(tǒng)停用嗎

反射是指在PHP運(yùn)行狀態(tài)中,擴(kuò)展分析PHP程序,導(dǎo)出或提取出關(guān)于類(lèi)、方法、屬性、參數(shù)等的詳細(xì)信息,包括注釋。這種動(dòng)態(tài)獲取的信息以及動(dòng)態(tài)調(diào)用對(duì)象的方法的功能稱(chēng)為反射API。反射是操縱面向?qū)ο蠓缎椭性P偷腁PI,其功能十分強(qiáng)大,可幫助我們構(gòu)建復(fù)雜,可擴(kuò)展的應(yīng)用。

其用途如:自動(dòng)加載插件,自動(dòng)生成文檔,甚至可用來(lái)擴(kuò)充PHP語(yǔ)言。

php反射api由若干類(lèi)組成,可幫助我們用來(lái)訪問(wèn)程序的元數(shù)據(jù)或者同相關(guān)的注釋交互。借助反射我們可以獲取諸如類(lèi)實(shí)現(xiàn)了那些方法,創(chuàng)建一個(gè)類(lèi)的實(shí)例(不同于用new創(chuàng)建),調(diào)用一個(gè)方法(也不同于常規(guī)調(diào)用),傳遞參數(shù),動(dòng)態(tài)調(diào)用類(lèi)的靜態(tài)方法。

反射api是php內(nèi)建的oop技術(shù)擴(kuò)展,包括一些類(lèi),異常和接口,綜合使用他們可用來(lái)幫助我們分析其它類(lèi),接口,方法,屬性,方法和擴(kuò)展。這些oop擴(kuò)展被稱(chēng)為反射。

下面的程序使用Reflection來(lái)構(gòu)造"CREATE TABLE"的sql語(yǔ)句。如果你不是很熟悉反射機(jī)制,可以從這個(gè)程序中看看反射的魅力與作用。

?php
/**
 * Creates an SQL 'Create Table' based upon an entity
 * @author Chris Tankersley chris@ctankersley.com>
 * @copyright 2010 Chris Tankersley
 * @package PhpORM_Cli
 */
class PhpORM_Cli_GenerateSql
{
  /**
   * Use a MySQL database
   */
  const MYSQL = 'mysql';
  /**
   * Use a SQLite database
   */
  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 database we are generating
   * @var string
   */
  protected $_type;
  /**
   * Sets 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 = self::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_match($this->_regexes['type'], $property->getDocComment(), $matches);
    if(count($matches) == 2) {
      if(array_key_exists($matches[1], $this->_validTypes)) {
        $type = $this->_validTypes[$matches[1]];
        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;
      } else {
        throw new Exception('Type "'.$matches[1].'" is not a supported SQL type');
      }
    } else {
      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].')';
    } else {
      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';
    } else {
      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(), '_') === false) {
        $definitions[] = $this->_getDefinition($property);
      }
    }
    $columns = implode(",n", $definitions);
    $sql = "CREATE TABLE ".$this->_tableName." (".$columns.")";
    if($this->_type == self::MYSQL) {
      $sql .= " ENGINE=MYISAM";
    }
    return $sql.";";
  }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

您可能感興趣的文章:
  • 深入解析PHP底層機(jī)制及相關(guān)原理
  • PHP中的異常處理機(jī)制深入講解
  • PHP底層運(yùn)行機(jī)制與工作原理詳解
  • php7 錯(cuò)誤處理機(jī)制修改實(shí)例分析
  • PHP的Trait機(jī)制原理與用法分析
  • PHP命名空間與自動(dòng)加載機(jī)制的基礎(chǔ)介紹
  • PHP session垃圾回收機(jī)制實(shí)例分析
  • PHP進(jìn)階學(xué)習(xí)之類(lèi)的自動(dòng)加載機(jī)制原理分析
  • PHP進(jìn)階學(xué)習(xí)之垃圾回收機(jī)制詳解
  • PHP簡(jiǎn)單驗(yàn)證碼功能機(jī)制實(shí)例詳解
  • PHP SESSION機(jī)制的理解與實(shí)例
  • PHP析構(gòu)函數(shù)destruct與垃圾回收機(jī)制的講解
  • 詳解PHP的執(zhí)行原理和流程

標(biāo)簽:涼山 昭通 十堰 九江 甘肅 遼陽(yáng) 梅河口 韶關(guān)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PHP反射機(jī)制來(lái)構(gòu)造CREATE TABLE的sql語(yǔ)句》,本文關(guān)鍵詞  使用,PHP,反射,機(jī)制,來(lái),構(gòu)造,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用PHP反射機(jī)制來(lái)構(gòu)造CREATE TABLE的sql語(yǔ)句》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于使用PHP反射機(jī)制來(lái)構(gòu)造CREATE TABLE的sql語(yǔ)句的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 临澧县| 贵港市| 读书| 宾阳县| 慈利县| 怀宁县| 营山县| 赫章县| 甘孜县| 武平县| 长兴县| 宜兰市| 开封市| 屏南县| 三门峡市| 绍兴县| 集贤县| 池州市| 东源县| 张家界市| 建始县| 孟州市| 大港区| 宁陕县| 泗洪县| 阿荣旗| 澎湖县| 富宁县| 密云县| 湘乡市| 甘南县| 万安县| 景洪市| 华亭县| 图们市| 武义县| 法库县| 新野县| 梨树县| 芒康县| 吉木乃县|