duanban4769 2013-06-22 15:14
浏览 31
已采纳

php中mysql的程序和面向对象实现的差异? [关闭]

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.

  • 写回答

3条回答 默认 最新

  • drecy22400 2013-06-22 15:26
    关注

    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.

    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']);
        }
    }
    

    This is a very shallow example.

    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);
    

    By the way I categorically prefer PDO over mysqli

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算