You are here: OOP PHP with MySQLi « PHP Programming Language « IT news, forums, messages
OOP PHP with MySQLi

Posted by Citrus on 01/31/06 16:04

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>";
}


?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация