简体   繁体   English

如何将mysqli类扩展到其他类方法中

[英]How to extend mysqli class into other class methods

Hey sorry for the stupid question. 嘿抱歉这个愚蠢的问题。 I have read lots about oop in php so i decided to try. 我在php中阅读了很多关于oop的内容,所以我决定尝试一下。 i have a blog running procedural php and all works fine but i have mysql not mysqli on the code and i decided to upgrade to the new mysqli. 我有一个运行程序性PHP的博客,一切正常,但我有mysql而不是代码上的mysqli,我决定升级到新的mysqli。 the problem here is really query the database to get results. 这里的问题实际上是查询数据库以获得结果。 i have been on the problem for two days now. 我已经解决了这个问题两天了。 here's a code example of what i want to implement. 这是我想要实现的代码示例。

class myDB extends mysqli
{
  //perform mysqli connection to host here and return  $connection_handle
}

class siteConfig
{
   private function getConfig($id)
  {
   $res = $connection_handle->query("SELECT * from config where id='1'");
   $row = $res->fetch_assoc();
   return $row['option'];
  }

}

on the main index file i would do this 在主索引文件上,我会这样做

$c = new siteConfig();
echo $c->getConfig(1);
//and this will return the result of the query.

also please pardon my programming logic and still a very terrible newbie to the oop world 也请原谅我的编程逻辑,对oop世界来说仍然是一个非常可怕的新手

any ideas will be helpful thank you. 任何想法都会有所帮助,谢谢。

PS PS

this is working example of the code am using 这是我正在使用的代码的工作示例

$db_link = mysqli_connect(DB,HOST,DB_USER,DB,PASS,DB_NAME) or die(mysql_error());

class myDB extends mysqli
{
  public function getConfig($id, $db_link) // thanks cbuckley
{
 $db_link = $this->db_link;
     $res = mysqli_query($this->db_link,"SELECT * from config where id='1'");
     $row = mysqli_fetch_array($res);
     return $row['option'];
}    
}

i have defined the constants already and the connection is successful but the select is not working. 我已经定义了常量,连接成功,但是选择不起作用。 on the index page this goes [code] include('inc/dbc.php'); 在索引页面上,这是[code] include('inc / dbc.php');

 $db = new MyDB();
 echo $db->getConfig(1, $db_link);  

please let me know where am missing it or if possible refactor the code for me where neccessary 请告诉我丢失的地方,或者如有可能,请在必要时为我重构代码

Why not just extend the mysqli class and add your functions to myDB? 为什么不扩展mysqli类并将您的函数添加到myDB?

class myDB extends mysqli
{
   public function getConfig($id) // thanks cbuckley
  {
   $res = $this->query("SELECT * from config where id='1'");
   $row = $res->fetch_assoc();
   return $row['option'];
  }    
}

$db = new myDB;
$db->getConfig($id);

You can call any mysqli function inside myDb by using the $this keyword (which refers to itself and since "it self" extends mysqli, it will have all the functions mysqli has + the ones you add) 你可以使用$ this关键字调用myDb中的任何mysqli函数(它指的是自己,因为“它自我”扩展了mysqli,它将拥有mysqli所有的函数+你添加的函数)

It's good to keep the separation between your database connection and any business logic, so your example is good. 保持数据库连接和任何业务逻辑之间的分离是很好的,所以你的例子很好。 There are just a couple of extra things that need sorting out, namely how the application logic has access to the database handler. 还有一些额外的东西需要整理,即应用程序逻辑如何访问数据库处理程序。 Here's an example: 这是一个例子:

class MyDB extends mysqli {
    // any custom DB stuff here
}

class SiteConfig {
    protected $db;

    public function __construct(MyDB $db) {
        $this->db = $db;
    }

    public function getConfig($id) {
        $statement = $this->db->prepare('SELECT * FROM config WHERE id = ?');
        $statement->bind_param('i', $id);

        if ($statement->execute()) {
            $row = $statement->get_result()->fetch_assoc();
            return $row['option'];
        }
    }
}

$db = new MyDB('host', 'user', 'password', 'db');
$config = new SiteConfig($db);
var_dump($config->getConfig(1));

A couple of points of interest here: 这里有几个兴趣点:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM