【问题标题】:PHP OOP - How to inherit mysqli methods to other class?PHP OOP - 如何将 mysqli 方法继承到其他类?
【发布时间】:2012-12-26 05:55:42
【问题描述】:

所以我正在尝试一种简单的方法来管理一堆 mysql 连接。

我有 2 个基础课程:serverConnections

服务器只是设置为保存您所看到的一行数据。 (有更好的方法吗?)

在连接中,我只想通过它的 ID 调用连接并创建连接,并能够查询它。

这适用于 mysql 库,但是当我尝试使用 mysqli 库时,它不会将 $mysqli 传递给 $msi 作为连接句柄。 $mysqli->query() 有效,但 $msi->query() 无效。

   PHP Fatal error:  Call to undefined method Connections::query()

我是否需要使用 mysqli 扩展/实现我的 Connections 类或以不同的方式返回 $mysqli

谢谢!

    <?php
    class server {
        public $id, $type, $subid, $host, $user, $pw, $port, $db;

        public function __construct($id,$type,$subid,$host,$user,$pw,$port,$db) {
            $this->id = $id;
            $this->type = $type;
            $this->subid = $subid;
            $this->host = $host;
            $this->user = $user;
            $this->pw = $pw;
            $this->port = $port;
            $this->db = $db;
        }
    }

    class Connections {
        public $servers;

        function __construct($id) {
            $this->servers[] = new server("mysql","mysql","main","hostname","username","password","3306","dbname");
            $this->servers[] = new server("mysql2","mysqli","main","hostname","username","password","3306","dbname");

            $rt = null;
            foreach($this->servers as $server) {
                if ($id == $server->id) {
                    $rt = $server;
                    break;
                }
            }

            if($rt->type == "mysql"){
                $con = mysql_connect($rt->host,$rt->user,$rt->pw);
                mysql_select_db($rt->db, $con);
                    if($con) { return $con; }
            }
            elseif($rt->type == "mysqli"){
                $mysqli = new mysqli($rt->host, $rt->user, $rt->pw, $rt->db, $rt->port);
                        if ($mysqli->connect_errno) {
                              echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                        }
                        else{
                              return $mysqli;
                        }
            }

        }
    }

    /*
    ### this works
    $nc = new Connections("mysql");
    $q = "select 1+2 as asdf";
    $r = mysql_query($q);
    while($row = mysql_fetch_array($r)){
        echo $row['asdf']."\n";
    }
    */

    #### this does not work
    $msi = new Connections("mysql2");
    $res = $msi->query("SELECT 2+2 as asdf");
    while($row = $res->fetch_assoc()){
        echo $row['asdf']."\n";
    }
    ?>
#

编辑 -> 在另一篇帖子 here

的帮助下,我能够以不同的方式完成这项工作

这是我修改后的代码:

<?php
class server {
    public $id, $type, $subid, $host, $user, $pw, $port, $alt; 
    public function __construct($id,$type,$subid,$host,$user,$pw,$port,$alt) {
        $this->id = $id;
        $this->type = $type;
        $this->subid = $subid;
        $this->host = $host;
        $this->user = $user;
        $this->pw = $pw;
        $this->port = $port;
        $this->alt = $alt;
    }  
}

class MySQLiContainer extends SplObjectStorage{
    var $server, $servers;
    function __construct($id) {
        $this->servers[] = new server("mysql","mysql","main","hostname","username","password","3306","dbname");
        $this->servers[] = new server("mysql2","mysqli","main","hostname","username","password","3306","dbname");

         foreach($this->servers as $server) {
                if ($id == $server->id) {
                    $this->server = $server;
                    break;
                }
            }
    }
  public function newConnection() {

    $mysqli = new mysqli($this->server->host, $this->server->user, $this->server->pw, $this->server->alt, $this->server->port);
    $this->attach($mysqli);
    return $mysqli;
  }
}

//usage
$mysqliContainer = new MySQLiContainer("mysql2");
$c1 = $mysqliContainer->newConnection();
$res = $c1->query('SELECT 2+4 as asdf');
while($row = $res->fetch_assoc()){
    echo $row['asdf']."\n";
}
echo $c1->host_info . "\n";

$ms2 = new MySQLiContainer("mysql");
$c2 = $ms2->newConnection();
$r = $c2->query('SELECT 2+4 as dfdf');
while($ra = $r->fetch_assoc()){
    echo $ra['dfdf']."\n";
}
echo $c2->host_info . "\n";
?>

【问题讨论】:

    标签: php class methods mysqli


    【解决方案1】:

    您的代码基于一个错误的前提:您可以从构造函数返回任何内容。

    你不能从构造函数返回任何东西。 new 运算符的结果始终是该类的一个实例。

    如果不清楚,请改写一下。您不能从构造函数返回 任何东西,因为 return 的值已被丢弃。

    你似乎想做的是extendmysqli。

    但是,您实际上并不想这样做,主要有两个原因:

    1. mysqli 无法将相关类(如语句句柄或结果句柄)作为派生类返回。这会严重限制功能。如果您真的想扩展数据库处理程序,请使用PDO
    2. 您为什么要编写自己的数据库抽象库?其中有几十个。去看看 Zend Db 或任何 Doctrine 使用的东西。它们将对您更有用 - 由合格的第三方创建并经过良好测试。

    【讨论】:

    • 感谢您澄清我对 __construct() 方法的误解。我正在尝试编写自己的数据库抽象库,因为我有几个站点在多个负载平衡的 EC2 实例后面使用共享的 Amazon RDS 实例。我想以某种方式对数据进行分片,从 RDS 实例中提取非关键信息并在 EC2 实例上运行,以增加我们的整体连接容量,同时不增加当前成本。
    • 我不羡慕这项任务。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2018-01-25
    • 1970-01-01
    • 2013-02-11
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多