当前位置:Gxlcms > PHP教程 > PHP面向对象中的标识对象

PHP面向对象中的标识对象

时间:2021-07-01 10:21:17 帮助过:19人阅读

  1. /*
  2. 标识对象模式
  3. 这个模式主要功能就是创建sql语句中的wehre条件字符串的,下面直接看代码和注释:
  4. */
  5. namespace woo\mapper;
  6. //字段对象
  7. class Field {
  8. protected $name = null; //字段名称
  9. protected $operator = null; //操作符
  10. protected $comps = array(); //存放条件的数组
  11. protected $incomplete = false; //检查条件数组是否有值
  12. function __construct ($name){
  13. $this->name= $name;
  14. }
  15. //添加where 条件
  16. function addTest($operator,$value){
  17. $this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);
  18. }
  19. //获取存放条件的数组
  20. function getComps(){
  21. return $this->comps;
  22. }
  23. function isIncomplete(){
  24. return empty($this->comps);
  25. }
  26. }
  27. //标识对象
  28. class IdentityObject {
  29. protected $currentfield = null; //当前操作的字段对象
  30. protected $fields = array(); //字段集合
  31. private $and = null;
  32. private $enforce = array(); //限定的合法字段
  33. function __construct($field = null, array $enforce = null){
  34. if(!is_null($enforce)){
  35. $this->enforce = $enforce;
  36. }
  37. if(!is_null($field)){
  38. $this->field($field);
  39. }
  40. }
  41. //获取限定的合法字段
  42. function getObjectFields(){
  43. return $this->enforce;
  44. }
  45. //主要功能为设置当前需要操作的对象
  46. function field($fieldname){
  47. if(!$this->isVoid()&& $this->currentfield->isIncomplete()){
  48. throw new \Exception("Incomplete field");
  49. }
  50. $this->enforceField($fieldname);
  51. if(isset($this->fields[$fieldname]){
  52. $this->currentfield = $this->fields[$fieldname];
  53. } else {
  54. $this->currentfield = new Field($fieldname);
  55. $this->fields[$fieldname] = $this->currentfield;
  56. }
  57. return $this; //采用连贯语法
  58. }
  59. //字段集合是否为空
  60. function isVoid(){
  61. return empty($this->fields);
  62. }
  63. //检查字段是否合法
  64. function enforceField ($fieldname){
  65. if(!in_array($fieldname,$this->enforce) && !empty($this->enforce)){
  66. $forcelist = implode(',',$this->enforce);
  67. throw new \Exception("{$fieldname} not a legal field {$forcelist}");
  68. }
  69. }
  70. //向字段对象添加where条件
  71. function eq($value){
  72. return $this->operator("=",$value);
  73. }
  74. function lt($value){
  75. return $this->operator("<",$value);
  76. }
  77. function gt($value){
  78. return $this->operator(">",$value);
  79. }
  80. //向字段对象添加where条件
  81. private function operator($symbol,$value){
  82. if($this->isVoid){
  83. throw new \Exception("no object field defined");
  84. }
  85. $this->currentfield->addTest($symbol,$value);
  86. return $this; //采用连贯语法
  87. }
  88. //获取此类中所有字段对象集合的where条件数组
  89. function getComps(){
  90. $ret = array();
  91. foreach($this->fields as $key => $field){
  92. $ret = array_merge($ret,$field->getComps());
  93. }
  94. return $ret;
  95. }
  96. }
  97. //客户端代码
  98. $idobj = new IdentityObject ();
  99. $idobj->field("name")->eq("The Good Show")->field("start")->gt(time())->lt(time()+(24*60*60));
  100. $test = $idobj->getComps();
  101. var_dump($test);
  102. //
输出类似下面的内容 /* array{ array('name'=>'name','operator'=>'=','value'=>'The Good Show'), array('name'=>'start','operator'=>'>','value'=>'123456'), //123456表示time()函数输出的时间戳 array('name'=>'start','operator'=>'<','value'=>'123456') } */

以上就是PHP面向对象中的标识对象的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行