为什么面向对象的PHP与mysqli优于过程方法? [英] Why is object oriented PHP with mysqli better than the procedural approach?

查看:76
本文介绍了为什么面向对象的PHP与mysqli优于过程方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,我一直在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?

面向对象的方法

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

程序方法

<?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库仅是OOP.它没有程序接口.在最近的几个版本中,大多数其他新功能也没有添加到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.

这篇关于为什么面向对象的PHP与mysqli优于过程方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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