問題描述
直到最近,我一直在對 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.
這篇關于為什么帶有 mysqli 的面向對象 PHP 比過程方法更好?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!