Re: OOP PHP with MySQLi



Citrus wrote:

> Hey, i'm looking for the best possible way for connecting to my database
> without too much of an effort.
>
> The example works fine and fast. What i need to know is if it's safe,
> stable and the way to go. If anyone has a better way for connecting
> side-wide to something i'd like you to show me. I've had it working before
> using 'global' but people told me 'global' is bad programming.
>
> One of the possibilities would be to make the link and pass it on to every
> function and object but this is NOT an option because it makes my script
> less transparant and easy to read.
>
>
> <?php
>
> class My {
> private static $connection;
>
> function SQL() {
> if(empty(self::$connection)) {
> self::$connection = new mysqli("localhost", "user", "password",
> "db");
> }
> return self::$connection;
> }
> }
>
> class User {
> function __construct() {
> echo "user init<p>";
> }
>
> public function show() {
> $query = My::SQL()->query("SELECT * FROM user");
> while($row = $query->fetch_object()) {
> echo $row->login."<p>";
> }
> }
> }
>
> $user = new User();
> $user->show();
>
> //Works here too
> $query = My::SQL()->query("SELECT * FROM user");
> while($row = $query->fetch_object()) {
> echo $row->login."<p>";
> }
>
>
> ?>


What a lot of overhead and such.
I am glad I just use global $connection; whereever I need it.

begining of every script that need a db:
<? require_once('dnconnect.php'); ?>

then from a function:
function leaveThingsSimple(){
global $connection;
// do stuff here
}

Bad programming?
Says who?
And why?

Only use Objects if they add something to your code or structure somehow.
(Like PEAR:DB)
I really don't see the problem with making $connection global...

Regards,
Erwin Moller
.



Relevant Pages

  • Re: dialup solution (as seconary connection / iptables )
    ... ip-down is launched by pppd only after the PPP link no longer available ... first with MINICOM or is PAP and script not mutualy exclusive? ... do have to "make the connection first with MINICOM." ... If you use the Linux box and route the news IP address to it on each ...
    (comp.os.linux.networking)
  • Re: mysqli connections and oop
    ... need to do all processing in a script before I display a page that will ... But it will most probably NOT terminated the execution of the script. ... this means that NO lasting connection is build ... Why does it not carry the mysqli connection? ...
    (comp.lang.php)
  • Re: mysqli connections and oop
    ... And here is the script that execute this ... lose a mysqli connection upon the reload of the page? ...
    (comp.lang.php)
  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... How does it relate to e.g. a script performing a large mysql query? ... 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: Keeping pppd alive--how?
    ... >>wrote a script to dial up my ISP, wait 40 seconds, then start up Jay's ... Perhaps using the netstat command to do ... >options tab is an option to redial is connection is lost. ... My Linux machine is text only--no GUIs. ...
    (alt.os.linux.redhat)