在php中mysql的过程和面向对象实现中的差异? [英] Differences in procedural and object-oriented implementations of mysql in php?

查看:87
本文介绍了在php中mysql的过程和面向对象实现中的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php中实现mysql时,使用面向对象方法与过程方法是否有显着差异?在有关mysqli_query的php网站上,(( http://www.php.net /manual/en/mysqli.query.php ),它提供了两者的示例,我只想知道是否存在明显的性能差异,或者只知道何时使用它们.

Is there a significant difference in using an object oriented approach over a procedural approach when implementing mysql in php? On the php website about mysqli_query, (http://www.php.net/manual/en/mysqli.query.php), it provides an example of both, and I just want to know if there is any significant performance difference, or just know when to use each of them.

推荐答案

更好的答案是取决于".与任何事物一样,有许多不同的方法,您还应记住,使用对象的代码不一定是面向对象的,但仍可以按程序编写.同样,不使用对象的代码仍然可以模块化.

The answer to which one is better is "it depends." As with anything, there are a variety of different approaches and you should also keep in mind that code that uses objects is not necessarily object oriented but can still be written procedurally. In the same vein, code that does not use objects can still be modular.

不过,我每次都会选择使用mysqli类.性能没有显着差异.您可能不会意识到使用数据库类的某些优点,例如简化的多态性,因此,使用该类的唯一论据是我更喜欢语法.但是,我可能会建议您扩展或编写它,而不是直接使用mysqli.您只能在全班学习.

I would choose to use the mysqli class every time, though. There is no significant difference in performance. You probably won't realize some of the advantages of using a DB class such as simplified polymorphism, so my only argument for using the class is that I prefer the syntax. However, rather than use mysqli directly I would probably recommend that you extend or compose it. You can only do this with the class.

class DB extends mysqli {
    public function __construct() {
        parent::__construct($_SERVER['DB_HOST'],
            $_SERVER['DB_USER'], $_SERVER['DB_PASS']);
    }
}

这是一个很浅的例子.

我上面所说的多态性的一个例子是这样的:

An example of the polymorphism I was talking about above would be something like this:

class User implements DAO {
    private $db;
    public function __construct(DB $db) {
        $this->db = $db;
    }
}

//Testing code is simplified compared to using it in production
class TestDB extends DB {}
new User(new TestDB);
new User(new DB);


按照我绝对喜欢PDO而不是mysqli


By the way I categorically prefer PDO over mysqli

这篇关于在php中mysql的过程和面向对象实现中的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆