为什么使用 mysqli 的面向对象 PHP 比过程方法更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14710195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is object oriented PHP with mysqli better than the procedural approach?
提问by Stacy J
I have been using the procedural approach with mysql* until recently. Now I want to shift to mysqli and object oriented approach. Many online resources and books state that OOP is better than procedural even in case of PHP. After going through some online tutorials, I have written a small program that connects to the database and selects a value. I want to know why the object oriented approach is better? Plus is this the right way to code an OO php web page?
直到最近,我一直在使用 mysql* 的程序方法。现在我想转向 mysqli 和面向对象的方法。许多在线资源和书籍都指出,即使在 PHP 的情况下,OOP 也比程序化要好。在浏览了一些在线教程后,我编写了一个连接数据库并选择一个值的小程序。我想知道为什么面向对象的方法更好?另外,这是编写 OO php 网页的正确方法吗?
The object oriented approach
面向对象的方法
$host = "localhost";
$username = "root";
$password = "";
$dbname = "compdb";
@ $db = new mysqli($host, $username, $password, $dbname);
if(mysqli_connect_errno())
{
die("Connection could not be established");
}
$query = "SELECT company_id FROM company_basic_details WHERE company_name = 'ABC'";
$result = $db->query($query);
$total_num_rows = $result->num_rows;
echo "The Results Are : <br>";
while($row = $result->fetch_array())
{
echo $row['company_id'];
}
?>
The procedural approach
程序方法
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "compdb";
@ $db = mysqli_connect($host, $username, $password, $dbname);
if(mysqli_connect_errno())
{
die("Connection could not be established");
}
$query = "SELECT company_id FROM company_basic_details WHERE company_name = 'ABC'";
$result = mysqli_query($db, $query);
$total_num_rows = mysqli_num_rows($result);
echo "The Results Are : <br>";
while($row = mysqli_fetch_array($result))
{
echo $row['company_id'];
}
?>
回答by SDC
The main reason is that PHP is moving steadily in the direction of OO programming.
主要原因是PHP正在朝着OO编程的方向稳步前进。
There's nothing wrong with using mysqli_xxx()functions rather than the OOP equivalents; it is exactly the same as far as the code is concerned.
使用mysqli_xxx()函数而不是 OOP 等价物并没有错;就代码而言,它完全相同。
The only issue is that you'll be getting yourself further and further behind the curve in terms of what people think of as well-written PHP code.
唯一的问题是,就人们所认为的编写良好的 PHP 代码而言,您将越来越落后于曲线。
It's worth noting that the PDO library, which is considered the ideal for most DB code in PHP is OOP-only. It doesn't have a procedural interface. And nor do most of the other new features added to PHP in the last few versions. If you want to use PHP to its fullest, you need to know OOP anyway.
值得注意的是,被认为是 PHP 中大多数 DB 代码的理想选择的 PDO 库是仅面向对象的。它没有程序界面。并且在最后几个版本中添加到 PHP 的大多数其他新功能也没有。如果您想最充分地使用 PHP,无论如何您都需要了解 OOP。
There's also the point about the ability to create an extension class for your DB -- something like this:
还有一点是为你的数据库创建扩展类的能力——像这样:
class myDB extends mysqli {
.... your own stuff here to extend and improve the base mysqli class
}
Of course you can achieve the same thing with procedural code, but it's not as neat as the OOP way. And of course that's only relevant if you actually want to extend the class.
当然,您可以使用过程代码实现相同的目的,但它不如 OOP 方式简洁。当然,这仅在您确实想扩展该类时才相关。
However, as a first step, just moving from mysql_xxx()to mysqli_xxx()is a great start. Moving the whole way to using the OOP interface would be even better, but just switching to the mysqli functions is a good start.
然而,作为第一步,从mysql_xxx()到mysqli_xxx()是一个很好的开始。将整个方式转移到使用 OOP 接口会更好,但只是切换到 mysqli 函数是一个好的开始。
Using the procedural interface to begin with will certainly make the transition away from the old mysql_xx()functions easier, so if switching to the OOP interface is too much of a leap at the beginning, don't feel you have to do it all in one go. Start with a conversion to the procedural mysqlifunctions, then switch to the OOP methods later on; neither jump will be that big on its own.
一开始使用过程界面肯定会让旧mysql_xx()功能的转换更容易,所以如果一开始切换到 OOP 界面是一个太大的飞跃,不要觉得你必须一次完成所有的事情。从转换到过程mysqli函数开始,然后再转换到 OOP 方法;这两个跳跃本身都不会那么大。

