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']; } ?>
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']; } ?>
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. Object-oriented programming has several advantages over procedural programming: OOP is faster and easier to execute.
MySQLi provides a procedural way, much similar to the MySQL. This is the reason why developers coming from a MySQL background prefers using MySQLi. However, object-oriented programmers prefer PDO because of its compatibility with a large number of databases.
However, sometimes using object oriented programming requires more effort than it's worth. In fact, PHP was initially developed as a procedural language and only later extended to OOP. PHP programmers cannot agree on which style is preferable.
Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.
The main reason is that PHP is moving steadily in the direction of OO programming.
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.
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.
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 }
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With