首页 > 解决方案 > 带过滤器的数据库类

问题描述

我正在构建自己的 CMS,并为此使用了自己的数据库类。在某些框架中,有一种方法可以在另一个函数上执行一个函数

$firstValues = $this-db->query('your query')->first();

->first()我需要什么样的课程。php中有没有办法做到这一点?我需要使用抽象类吗?有人有例子吗?

如果您需要更多信息,请询问我!感谢您的帮助!

汤姆

标签: phpmysqloop

解决方案


您必须在方法之间传递 $this 。这里有一些例子:

Class Db {

   private $_query_result;

   public function query($query_string = '')
   {
         // make your query or smth


         // save for example array of results
         $this->_query_result = $your_query_result;
         return $this;
   }

   public function first() 
   {
         return array_shift($this->_query_result);
   }
}

推荐阅读