mysqli, OOP vs Procedural

前端 未结 3 1197
忘了有多久
忘了有多久 2020-12-17 19:41

I just saw this for the first time. I had no idea you can instantiate a mysqli class by doing something like

new mysqli( $host, $username, $password, $db )         


        
相关标签:
3条回答
  • 2020-12-17 19:55

    Most mysqli_* functions are aliases of the object methods (PHP internally allows that easily with an C macro, so many extensions use that approach). But the availability is also for familiarity, so users of the old mysql functions have an easier time adopting to the new interface. Using the OOP or procedural interface is really just style/preference.

    PDO is generally preferred nowadays, but it doesn't provide many mysql-specific features. So there might still be some use cases for the mysqli extension.

    0 讨论(0)
  • 2020-12-17 20:09

    To answer your questions from the practical point of view

    Is there a reason why most tutorials do it the procedural way?

    Enough to say that reasons are not technology related.

    What's the difference with mysqli procedural vs oop? Is there any?

    No.

    Do I have to install any extensions

    No (save for mysqli itself, of course).

    Also, when is it a good idea to use PDO?

    Yes. As a matter of fact PDO would be the only sane choice.

    You have to understand that set of mysqli functions is just a low level API, not intended to be used as is. One have to implement a wrapper upon mysqli, and then use this wrapper's methods. While PDO is such a wrapper already, offering help with performing many tasks. I wrote a guide on PDO, that emphasizes its benefits and discloses some superstitions, where, I hope, you will find answers for many questions you surely have.

    0 讨论(0)
  • 2020-12-17 20:15

    99% of PHP tutorials on the internet are outdated, advocate terrible code or are written by people that probably shouldn't teach PHP (and commonly all of the above). You should find the handful of good ones.

    PDO is generally favoured over mysqli by developers, including me. For one, it can handle many more databases than just MySQL.

    0 讨论(0)
提交回复
热议问题