Why Is Object Oriented PHP with MySQLi Better Than the Procedural Approach

Why is object oriented PHP with mysqli better than the procedural approach?

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.

Differences in procedural and object-oriented implementations of mysql in php?

The answer to which one is better is "it depends." As with anything, there are a variety of different approaches and you should also keep in mind that code that uses objects is not necessarily object oriented but can still be written procedurally. In the same vein, code that does not use objects can still be modular.

I would choose to use the mysqli class every time, though. There is no significant difference in performance. You probably won't realize some of the advantages of using a DB class such as simplified polymorphism, so my only argument for using the class is that I prefer the syntax. However, rather than use mysqli directly I would probably recommend that you extend or compose it. You can only do this with the class.

class DB extends mysqli {
public function __construct() {
parent::__construct($_SERVER['DB_HOST'],
$_SERVER['DB_USER'], $_SERVER['DB_PASS']);
}
}

This is a very shallow example.

An example of the polymorphism I was talking about above would be something like this:

class User implements DAO {
private $db;
public function __construct(DB $db) {
$this->db = $db;
}
}

//Testing code is simplified compared to using it in production
class TestDB extends DB {}
new User(new TestDB);
new User(new DB);

By the way I categorically prefer PDO over mysqli

Difference between calling a function by procedural or object oriented style

No, there is no difference. The procedural way is pretty much just a wrapper around the OO API. Historically it was included to allow developers for whom OO was a complete mystery to transition to a better alternative from the mysql API.

For all intents and purposes mysqli_num_rows does this:

function mysqli_num_rows(mysqli_result $result) {
return $result->num_rows;
}

procedural style and object oriented style

famous for not knowing how to asking questions

Sad but true.

If some of your code is not working, it's better to post this very code instead of writing a long literary explanation.

When you're making it

while ($rowinfo = mysqli_fetch_assoc($object->query($sql)))

it indeed makes an infinite loop, because you are making your SQL query run over and over.

The problem has nothing to do with object syntax, it will remain the same with procedural as well. You are just supposed to use the result in stead of calling query again.

In essence, you have to run your query **only once. **

Which makes your other code snippet wrong as well. It should be

$object = new mysqli('localhost', 'readmode', 'p@5sW0rd', 'practices');
$sql = 'SELECT * FROM paintings';
$result = $object->query($sql);
if ($object->connect_error) {
$error = $object->connect_error;
} else {
$num_rows = mysqli_num_rows($result);
}

otherwise it will run your query twice.

Is it acceptable to use a mix of object oriented style with procedural style in coding PHP?

While how you code is entirely your decision and unique style, I'd say there are a few factors to consider when deciding on procedural, object oriented, or mixed.

Program specifications -

Primarily, if you are on a team, writing the program for someone else, or following your own specifications, consider whether or not the choice has already been made.

Availability -

Let's face it. Sometimes the best libraries are available in either object oriented or procedural, and not both. In such a case, changing from one style would require using a completely different library, or building a class or function library yourself. An available library may save you time, with the only offset cost being a procedural function in a primarily object oriented program, or vice versa.

Familiarity -

Similar to availability, you may be more familiar with a certain class or set of functions. While you may have time to stop and learn a new class module to add to your knowledge, you may be able to save time by using a procedural library that you've already learned and thoroughly tested. So if you are working on a timeline, you may want to go with the more familiar library. However, if you are researching and learning, then you may want to take the time to read documentation, install, and test a new solution.

Data handling and speed -

One more factor to muse about is how are you handling the data. If the data is within the class, then the class will likely have methods to operate on the data. In such a situation, procedural programming would require obtaining the data from the class or object, operating on the data, and then updating the object. A better design would be to include the function in the object in my opinion.

However, if all of your data handling is outside of the class, then using a function may be faster. If you wanted to use a class method, you would have to load the class, and possibly create an object. Even static methods may be slower than a function. So if speed is a consideration, such as in a loop, then consider how many steps your program and PHP has to go through to get to the function, class, or object.

Looking ahead -

If you are wanting to select between procedural or object-oriented programming, then try to predict what will be most useful in the future. I've found object-oriented programming to be very useful for creating reusable code. I've found procedural programming to be very useful for command line code and organizing and using objects. It's likely these will stay the same as computer science evolves, and so work I've done previously is more likely to be useful again.

In contrast, some libraries and programming languages may encourage a style. PHP supports both styles. But if my overall impression is accurate, then PHP has been moving in the direction of object-oriented styles. If selecting between PHP functions and objects, look and see what version of PHP the functions were created for. Also check to see if any of the procedural functions are depreciated or going to become obsolete. If so, use the object-oriented approach, as this will make your program more useful when those procedural functions are no longer supported.

Hope this provides some considerations. Thanks.

Which php `fetch_array` way should i use

Actually $result->fetch_array()) and mysqli_fetch_array($result)) both do the same thing. But using $result->fetch_array()) is object oriented style and mysqli_fetch_array($result)) is traditional procedural style.

Also mysqli_fetch_array() will collect the data from database as an array.

You can use any one of them. But I prefer object oriented style.



Related Topics



Leave a reply



Submit