为什么带有 mysqli 的面向对象 PHP 比过程方法更好?

2021-12-25 00:00:00 object php mysqli procedural-programming

直到最近,我一直在对 mysql* 使用过程方法.现在我想转向 mysqli 和面向对象的方法.许多在线资源和书籍都指出,即使在 PHP 的情况下,OOP 也比过程化要好.在浏览了一些在线教程后,我编写了一个连接数据库并选择一个值的小程序.我想知道为什么面向对象的方法更好?另外,这是编写 OO php 网页的正确方法吗?

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?

面向对象的方法

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

程序方法

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

推荐答案

主要原因是PHP正在向OO编程的方向稳步前进.

The main reason is that PHP is moving steadily in the direction of OO programming.

使用 mysqli_xxx() 函数而不是 OOP 等价函数没有任何问题;就代码而言,完全一样.

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.

唯一的问题是,就人们认为编写良好的 PHP 代码而言,您将越来越落后于曲线.

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 中大多数 DB 代码的理想选择的 PDO 库是仅面向对象的.它没有程序界面.并且在最后几个版本中添加到 PHP 的大多数其他新功能也没有.如果您想充分利用 PHP,无论如何您都需要了解 OOP.

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.

还有一点是为您的数据库创建扩展类的能力——就像这样:

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
}

当然你可以用过程代码来实现同样的事情,但它不像 OOP 方式那么简洁.当然,这仅在您确实想扩展该类时才有意义.

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.

然而,作为第一步,从 mysql_xxx() 移动到 mysqli_xxx() 是一个很好的开始.将整个方式转移到使用 OOP 接口会更好,但只是切换到 mysqli 函数是一个好的开始.

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_xx() 函数的过渡变得更容易,所以如果一开始切换到 OOP 接口是一个太大的飞跃,请不要不觉得你必须一口气做完.从转换为过程 mysqli 函数开始,然后稍后切换到 OOP 方法;这两个跳跃本身都不会那么大.

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 mysqli functions, then switch to the OOP methods later on; neither jump will be that big on its own.

相关文章