3

Is there a significant difference in using an object oriented approach over a procedural approach when implementing mysql in php? On the php website about mysqli_query, (http://www.php.net/manual/en/mysqli.query.php), it provides an example of both, and I just want to know if there is any significant performance difference, or just know when to use each of them.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
JPeroutek
  • 558
  • 1
  • 7
  • 20
  • I'm a n00b myself, but from what I understand, procedural approach is easier to read. Other than that, there's nothing much. (correct me if I'm wrong) – dayuloli Jun 22 '13 at 15:18
  • 2
    Performance: no. Codeflow: yes. Pick the one suited for _your_ project. Newer OO code likely benefits more of the OO approach then existing / older procedural code. The ability to extend & better control the object in an OO approach is worth a lot in larger projects. – Wrikken Jun 22 '13 at 15:19
  • The thing is, I am writing a website basically from scratch for the company I am interning at. Would it be better to go ahead and use the OO method? Is it more future-proof? Sorry if I am unclear. – JPeroutek Jun 22 '13 at 15:20
  • OO all the way in my opinion. Although you should look into PDO istead of MySQLi. –  Jun 22 '13 at 15:25
  • 2
    I’d suggest not commenting with statements of fact if you’re a self-professed n00b. Opinions are fine, but phrases like, “Other than that, there’s nothing much” isn’t. Especially in the context of the question asked. – Martin Bean Jun 22 '13 at 15:35

3 Answers3

4

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

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • thank you for a detailed answer, with a good explanation and example. I would just hit the checkmark, but I am going to wait a while, just incase a better answer comes. – JPeroutek Jun 22 '13 at 15:28
0

As you are an intern it would be advisable to find out who is going to maintain the code in the future.

  1. Nobody in company.Go with OOP ,if you are happy with this, as OOP is the current practice.
  2. In House. Go with current practice,hopefully OOP
david strachan
  • 7,174
  • 2
  • 23
  • 33
  • Thank you for your answer. I think we are doing it in house, because that is why I am making the site, the last group they hired switched cms systems every two weeks, so we are seeking long-term stability. – JPeroutek Jun 23 '13 at 00:52
-5

Nope, there is no difference.

But you're overlooking way more important thing: no raw mysqi API functions have to be used in the application code, neither OOP or procedural.

There is one thing unknown to PHP users: API calls aren't intended to be used as is. They have to be wrapped in some sort of library. Without it mysqli is just a pain in the back, it require 2 times more code than old good mysql ext.

So, it does matter if you are using such a layer, but it doesn't matter if it is written using procedural or OOP inside.

Yet this library ought to be built in the form of class. There is just no other way.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • So you're saying that one should create a class where all the MySQLi functions are just wrapped? Or are you talking about more of a model class? I don't really see how MySQLi would require twice the amount of code compared to the original MySQL extension... –  Jun 22 '13 at 15:45
  • I am talking of the first one. but I have to admit, to understand a DRY principle one have to have certain experience and some projects done at hand. – Your Common Sense Jun 22 '13 at 16:04