PEAR and objects - how to construct?

From: Date: Sun, 29 Dec 2002 09:15:31 +0000
Subject: PEAR and objects - how to construct?
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3102@lists.php.net to get a copy of this message
I am going to build a new website and in my research I found PEAR and now I am probably stuck :) I have a problem about how to write an sql class using the DB package. I want to write an sql class which constructor opens a db-connection. The destructor close the db-connection: Example: class Sql extends PEAR { var $db; function Sql ($dsn) { $this->PEAR(); $this->db =& DB::connect($dsn,true); if (!DB::isError($this->db)) { // code to handle the connection error } } function _Sql() { if(!DB::isError($this->db)){ $this->db->disconnect(); } } } Let's say that you are writing a guestbook. The script contains several classes which uses the Sql class in order to access the database. My thinking here is first to create the sql object (and connect to the database) and pass the reference to the Sql object to all other objects in order to let them use the same database connection. Example: Class Message extends PEAR { var $db_obj; function (&$db_obj); { $this->PEAR(); $this->$db_obj =& $db_obj; } function readMessage($id) { $sql = "SELECT * FROM w3t_Users where id = $id"; $result = $db_obj->db->query($sql); if(!DB::isError($result) && $result->numRows()) { return $result->fetchRow(); } } My question is about how to avoid dribbling inside another object: $result = $db_obj->db->query($sql); I don't think that the above statement is good OOO programming. One way is to add a method in the Sql Class: function return dbHandle() { return $this->db; } and pass that handle as a reference to all other classes? Do you think this is a good solution or am I on the wrong path?

« previous php.pear.general (#3102) next »