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

主頁 > 知識庫 > 使用PHP反射機制來構造CREATE TABLE的sql語句

使用PHP反射機制來構造CREATE TABLE的sql語句

熱門標簽:保定crm外呼系統運營商 海南人工外呼系統有效果嗎 西區企業怎么做地圖標注入駐 智能電話機器人排名前十名南京 阿里云400電話申請加工單 九江外呼系統 抖音有個地圖標注是什么意思 地下城堡2圖九地圖標注 七魚外呼系統停用嗎

反射是指在PHP運行狀態中,擴展分析PHP程序,導出或提取出關于類、方法、屬性、參數等的詳細信息,包括注釋。這種動態獲取的信息以及動態調用對象的方法的功能稱為反射API。反射是操縱面向對象范型中元模型的API,其功能十分強大,可幫助我們構建復雜,可擴展的應用。

其用途如:自動加載插件,自動生成文檔,甚至可用來擴充PHP語言。

php反射api由若干類組成,可幫助我們用來訪問程序的元數據或者同相關的注釋交互。借助反射我們可以獲取諸如類實現了那些方法,創建一個類的實例(不同于用new創建),調用一個方法(也不同于常規調用),傳遞參數,動態調用類的靜態方法。

反射api是php內建的oop技術擴展,包括一些類,異常和接口,綜合使用他們可用來幫助我們分析其它類,接口,方法,屬性,方法和擴展。這些oop擴展被稱為反射。

下面的程序使用Reflection來構造"CREATE TABLE"的sql語句。如果你不是很熟悉反射機制,可以從這個程序中看看反射的魅力與作用。

?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.";";
  }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

您可能感興趣的文章:
  • 深入解析PHP底層機制及相關原理
  • PHP中的異常處理機制深入講解
  • PHP底層運行機制與工作原理詳解
  • php7 錯誤處理機制修改實例分析
  • PHP的Trait機制原理與用法分析
  • PHP命名空間與自動加載機制的基礎介紹
  • PHP session垃圾回收機制實例分析
  • PHP進階學習之類的自動加載機制原理分析
  • PHP進階學習之垃圾回收機制詳解
  • PHP簡單驗證碼功能機制實例詳解
  • PHP SESSION機制的理解與實例
  • PHP析構函數destruct與垃圾回收機制的講解
  • 詳解PHP的執行原理和流程

標簽:涼山 昭通 十堰 九江 甘肅 遼陽 梅河口 韶關

巨人網絡通訊聲明:本文標題《使用PHP反射機制來構造CREATE TABLE的sql語句》,本文關鍵詞  使用,PHP,反射,機制,來,構造,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用PHP反射機制來構造CREATE TABLE的sql語句》相關的同類信息!
  • 本頁收集關于使用PHP反射機制來構造CREATE TABLE的sql語句的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 东光县| 凤山县| 毕节市| 佛坪县| 会昌县| 武城县| 旌德县| 霍邱县| 新建县| 达孜县| 延寿县| 正宁县| 什邡市| 上思县| 东明县| 郓城县| 达尔| 方山县| 巴林左旗| 云霄县| 濮阳县| 宁陵县| 卓资县| 大姚县| 永昌县| 岳西县| 工布江达县| 昆明市| 三亚市| 丹棱县| 福清市| 美姑县| 连江县| 彰武县| 封丘县| 饶阳县| 山丹县| 济宁市| 通江县| 中牟县| 桐城市|