使用PHP反射机制来构造"CREATE TABLE"的sql语句
反射是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。
其用途如:自动加载插件,自动生成文档,甚至可用来扩充PHP语言。
php反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互。借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传递参数,动态调用类的静态方法。
反射api是php内建的oop技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性,方法和扩展。这些oop扩展被称为反射。
下面的程序使用Reflection来构造"CREATETABLE"的sql语句。如果你不是很熟悉反射机制,可以从这个程序中看看反射的魅力与作用。
*@copyright2010ChrisTankersley *@packagePhpORM_Cli */ classPhpORM_Cli_GenerateSql { /** *UseaMySQLdatabase */ constMYSQL='mysql'; /** *UseaSQLitedatabase */ constSQLITE='sqlite'; /** *Typesthatareallowedtohavealength *@vararray */ protected$_hasLength=array('integer','varchar'); /** *Regexesneededtopulloutthedifferentcomments *@vararray */ protected$_regexes=array( 'type'=>'/type=([a-z_]*)/', 'length'=>'/length=([0-9]*)/', 'default'=>'/default="(.*)"/', 'null'=>'/null/', ); /** *Typesthatwesupport *@vararray */ protected$_validTypes=array( 'boolean'=>'BOOL', 'date'=>'DATE', 'integer'=>'INT', 'primary_autoincrement'=>'INTAUTO_INCREMENTPRIMARYKEY', 'text'=>'TEXT', 'timestamp'=>'TIMESTAMP', 'varchar'=>'VARCHAR', ); /** *Nameoftheclasswewillinterperet *@varstring */ protected$_className; /** *Nameofthetablewearegenerating *@varstring */ protected$_tableName; /** *Thetypeofdatabasewearegenerating *@varstring */ protected$_type; /** *Setsthenameoftheclassweareworkingwith *@paramstring$class *@paramstring$table_name *@paramstring$type */ publicfunction__construct($class,$table_name,$type=self::MYSQL) { $this->_className=$class; $this->_tableName=$table_name; $this->_type=$type; } /** *BuildsanSQLLineforaproperty *@paramReflectionProperty$property *@returnstring */ protectedfunction_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{ thrownewException('Type"'.$matches[1].'"isnotasupportedSQLtype'); } }else{ thrownewException('Found'.count($matches).'whencheckingTypeforproperty'.$property->getName()); } } /** *ExtractstheLengthfromaproperty *@paramReflectionProperty$property *@returnstring */ protectedfunction_getLength($property) { preg_match($this->_regexes['length'],$property->getDocComment(),$matches); if(count($matches)==2){ return'('.$matches[1].')'; }else{ return''; } } /** *DeterminesifaPropertyisallowedtobenull *@paramReflectionProperty$property *@returnstring */ protectedfunction_getNull($property) { preg_match($this->_regexes['null'],$property->getDocComment(),$matches); if(count($matches)==1){ return'NULL'; }else{ return'NOTNULL'; } } /** *GeneratesablockofSQLtocreateatablefromanEntity *@returnstring */ publicfunctiongetSql() { $class=newReflectionClass($this->_className); $definitions=array(); foreach($class->getProperties()as$property){ if(strpos($property->getName(),'_')===false){ $definitions[]=$this->_getDefinition($property); } } $columns=implode(",n",$definitions); $sql="CREATETABLE".$this->_tableName."(".$columns.")"; if($this->_type==self::MYSQL){ $sql.="ENGINE=MYISAM"; } return$sql.";"; } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接