Php 使用PDO、_调用和preg_匹配自动创建访问器方法

Php 使用PDO、_调用和preg_匹配自动创建访问器方法,php,sql,oop,Php,Sql,Oop,我开始研究一种自动创建数据库访问器的方法。我正在寻找与PHP>=5.2兼容的东西。我第一次尝试的结果是: 我很好奇是否有人已经做过这件事或类似的事情,所以我不必这么做,但我也很好奇我是否在思考这件事是否会有所帮助时遗漏了什么。我的一般想法是,因为它扩展了PDO,所以当我需要更复杂的东西时,我总是可以回到正常的SQL查询。听起来像是在构建ORM的开端。这意味着现在是强制性链接的时候了: 听起来你正在建立一个ORM的开端。这意味着现在是强制性链接的时候了: 谢谢,看起来我正在重新发明这里列出的轮子

我开始研究一种自动创建数据库访问器的方法。我正在寻找与PHP>=5.2兼容的东西。我第一次尝试的结果是:


我很好奇是否有人已经做过这件事或类似的事情,所以我不必这么做,但我也很好奇我是否在思考这件事是否会有所帮助时遗漏了什么。我的一般想法是,因为它扩展了PDO,所以当我需要更复杂的东西时,我总是可以回到正常的SQL查询。

听起来像是在构建ORM的开端。这意味着现在是强制性链接的时候了:


听起来你正在建立一个ORM的开端。这意味着现在是强制性链接的时候了:


谢谢,看起来我正在重新发明这里列出的轮子:。我从一开始就担心收益递减的问题,但很高兴知道这不仅仅是因为我不知道如何让我担心。谢谢,看起来我正在重新发明这里列出的轮子:。我从一开始就担心收益递减的问题,但很高兴知道这不仅仅是因为我不知道如何让我担心。 class FancyPDOBase extends PDO{ /////////////////////////////////////////////////////////////////////////////// ////Magic /////////////////////////////////////////////////////////////////////////////// public function __call($method, $args){ if(preg_match('/get[A-Z]{1}[a-z_]*[A-Z]{1}[a-z_]*By[A-Z]{1}[a-z_]*/', $method)){ return $this->getFieldByFields($method, $args); }else if( preg_match('/get[A-Z]{1}[a-z_]*[A-Z]{1}[a-z_]*[A-Z]{1}[a-z_]*Array/', $method) ){ return $this->getPairArray($method); }//Add more expressions here. } /////////////////////////////////////////////////////////////////////////////// protected function getFieldByFields($method, $args){ // Create a series of value getters preg_match('/get([A-Z]{1}.)([A-Z]{1}.)By([A-Z]{1}.*)/', $method, $matches); $table = strtolower($matches[1]); $get = strtolower($matches[2]); preg_match_all('/[A-Z]{1}[^A-Z]*/', $matches[3], $split); $where = self::createWhereStatement($split, $args, $table); $query = "SELECT $get FROM $table $where"; $result = $this->query($query); if($result){ $r = $result->fetchAll(); if(count($r)==1){ return $r[0][0]; }else{ return $r; } }else{ return null; } } //Add more methods here. }