PDA

View Full Version : OOPs in php with sql?


duniyadnd
11-29-2002, 07:37 PM
I'm quite familiar Object Oriented Programming with Php, but when I mix it with getting information from databases, esp. those so easy to access such as SQL, my basics for OOP goes flying out the window and I start programming with no structure.

Does anyone know of any good articles online dealing with this sort of issue?

Thanks
Duniyadnd

firepages
11-30-2002, 12:27 AM
you could grab one of the many existing MySQL classes or DB abstraction classes that are out there, php has its own in PEAR ( http://pear.php.net ) [but many have issues with it;)]

Many would create the DB access class and just pass SQL to the DB object, so

$db= new database('localhost','blah','blah');

then pass that object around to the other classes , so

$yaks=new llama($vars,$db);

or you could instansiate your DB in a base class that you will be using.

<?
class base_class {
var $conn;
function base_class(){
$db = new database('mysql','localhost','root','','');
$db->select_db('database');
$this->conn=$db;
}
function some_method($SQL){
//where fetch_rows is a method of the database class//
$rets=$this->conn->fetch_rows($SQL);
return $rets;
}
}

$test= new base_class();
echo $test->some_method("SELECT * FROM table")
?>


I havent shown the database class && this is a real bad example but you can see where Its going