扩展 mysqli 并使用多个类

发布于 2024-09-01 23:54:04 字数 912 浏览 6 评论 0原文

我是 PHP oop 的新手。

我正在尝试创建类数据库并从中调用其他类。我的做法正确吗?

类数据库:

class database extends mysqli {

private $classes = array();

public function __construct() {
parent::__construct('localhost', 'root', 'password', 'database');
    if (mysqli_connect_error()) {
    $this->error(mysqli_connect_errno(), mysqli_connect_error());
    }
}

public function __call($class, $args) {
    if (!isset($this->classes[$class])) {
    $class = 'db_'.$class;
    $this->classes[$class] = new $class(); 
    }
return $this->classes[$class];
}

private function error($eNo, $eMsg) {
die ('MySQL error: ('.$eNo.': '.$eMsg);
}

}

类 db_users:

class db_users extends database {

public function test() {
echo 'foo';
}

}

以及我如何使用它

$db = new database();
$db->users()->test();

这是正确的方法还是应该以其他方式完成?

谢谢。

I'm new to PHP oop stuff.

I'm trying to create class database and call other classes from it. Am I doing it the right way?

class database:

class database extends mysqli {

private $classes = array();

public function __construct() {
parent::__construct('localhost', 'root', 'password', 'database');
    if (mysqli_connect_error()) {
    $this->error(mysqli_connect_errno(), mysqli_connect_error());
    }
}

public function __call($class, $args) {
    if (!isset($this->classes[$class])) {
    $class = 'db_'.$class;
    $this->classes[$class] = new $class(); 
    }
return $this->classes[$class];
}

private function error($eNo, $eMsg) {
die ('MySQL error: ('.$eNo.': '.$eMsg);
}

}

class db_users:

class db_users extends database {

public function test() {
echo 'foo';
}

}

and how I'm using it

$db = new database();
$db->users()->test();

Is it the right way or should it be done another way?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-08 23:54:04

你可以这样做,这没有什么错(我经常做类似的事情)。我唯一建议的是使用异常而不是死(这样你就可以安全地处理错误)...

protected function error($eNo, $eMsg, $extra = '') {
    throw new Exception('MySQL error: ['.$eNo.'] '.$eMsg.': '.$extra);
}

另外,我建议也重载查询方法

public function query($sql, $result_mode = MYSQLI_STORE_RESULT) {
    $result = parent::query($sql, $result_mode);
    if ($result === false) {
         $this->error($this->errno, $this->errstr, $sql);
    }
    return $result;
}

我还建议存储 $db 对象的副本儿童班级内部。所以:

class db_users extends database {
    protected $db = null;

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

    public function test() {
        echo 'foo';
    }
}

然后,在 __call 中:

if (!isset($this->classes[$class])) {
    $class = 'db_'.$class;
    $this->classes[$class] = new $class($this); 
}

You can do it that way, there's nothing wrong with that (I do something similar quite often). The only thing I would suggest is using exceptions instead of die (that way you can safely handle the error)...

protected function error($eNo, $eMsg, $extra = '') {
    throw new Exception('MySQL error: ['.$eNo.'] '.$eMsg.': '.$extra);
}

Plus, I'd suggest overloading the query method as well

public function query($sql, $result_mode = MYSQLI_STORE_RESULT) {
    $result = parent::query($sql, $result_mode);
    if ($result === false) {
         $this->error($this->errno, $this->errstr, $sql);
    }
    return $result;
}

I'd also suggest storing a copy of the $db object inside of the child class. So:

class db_users extends database {
    protected $db = null;

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

    public function test() {
        echo 'foo';
    }
}

Then, in __call:

if (!isset($this->classes[$class])) {
    $class = 'db_'.$class;
    $this->classes[$class] = new $class($this); 
}
从﹋此江山别 2024-09-08 23:54:04

这种创建类的工厂风格没有什么问题。我会在其中放置一些异常处理。

我唯一关心的是在子类中扩展数据库。

所以我修改如下:

public function __call($className, $args) {
    if (!isset($this->classes[$class])) {
        if(include_once('db_'.$class)) {
            $class = 'db_'.$class;
            $this->classes[$class] = new $class($this);
        } else {
            throw new Exception("Db class not found");
        }
    }

    return $this->classes[$class];
}

用户类为:

public class db_users {
    private $db;

    public __constructor($db) {
        $this->db = $db;
    }

    public function test() {
        return 'Foo';
    }
}

There is nothing wrong with this factory style for creating classes. I'd place a bit of exception handling in it.

My only other concern is extending database in your sub classes.

So I'd modify as follows:

public function __call($className, $args) {
    if (!isset($this->classes[$class])) {
        if(include_once('db_'.$class)) {
            $class = 'db_'.$class;
            $this->classes[$class] = new $class($this);
        } else {
            throw new Exception("Db class not found");
        }
    }

    return $this->classes[$class];
}

And the users class as:

public class db_users {
    private $db;

    public __constructor($db) {
        $this->db = $db;
    }

    public function test() {
        return 'Foo';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文