Re: PHP5 OOP
| From: | Matthew Sims | Date: | Tue, 10 Aug 2004 18:55:30 +0000 |
| Subject: | Re: PHP5 OOP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-193356@lists.php.net to get a copy of this message | ||
> Hello, I'm trying to get the hang of OOP here but can't quite figure
> out how to relate these classes. I've got one for the main project,
> one for the database, and one for a user. Somehow I need to get the
> user access to the database, without cumbersome constructor calls
> involving a copy of the instance of the class itself.
<snip class>
>
> I tried extending the user class from the project class, but that
> didn't work, because the $db var was empty. I tried changing it to
> static, but it didn't inherit the $db variable for some reason. So,
> how can I make this work, so I can write a bunch of classes that can
> blindly use $this->db or something similar without having to worry
> about setting it in the constructor? I thought about setting it as a
> global, but that didn't seem very... OOP.
>
> --
> Joel Kitching
> http://midgardmanga.keenspace.com/
>
This is a great class to learn how OO works in PHP5.
class DB_Mysql {
protected $user; // Database username
protected $pass; // Database password
protected $dbhost; // Database host
protected $dbname; // Database name
protected $dbh; // Database handle
public function __construct($user, $pass, $dbhost, $dbname) {
$this->user = $user;
$this->pass = $pass;
$this->dbhost = $dbhost;
$this->dbname = $dbname;
}
protected function connect() {
$this->dbh = @mysql_connect($this->dbhost, $this->user, this->pass);
if (!is_resource($this->dbh)) {
throw new Exception("Cannot connect to the database");
}
if (!mysql_select_db($this->dbname, $this->dbh)) {
throw new Exception("No such database by that name");
}
}
public function execute($query) {
if (!$this->dbh) {
$this->connect();
}
$ret = mysql_query($query, $this->dbh);
if (!$ret) {
throw new Exception("There is an issue with the query string");
} elseif (!is_resource($ret)) {
return TRUE;
} else {
$stmt = new DB_MysqlStatement($this->dbh, $query);
$stmt->result = $ret;
return $stmt;
}
}
}
class DB_MysqlStatement {
public $result;
public $query;
protected $dbh;
public function __contruct($dbh, $query) {
$this->query = $query;
$this->dbh = $dbh;
if (!is_resource($dbh)) {
throw new Exception("Cannot connect to the database");
}
}
public function fetch_row() {
if (!$this->result) {
throw new Exception("There is an issue with the query string");
}
return mysql_fetch_row($this->query);
}
public function fetch_assoc() {
return mysql_fetch_assoc($this->result);
}
public function fetchall_assoc() {
$retval = array();
while ($row = $this->fetch_assoc()) {
$retval[] = $row;
}
return $retval;
}
}
Then call it like:
$dbhObj = new
DB_Mysql("user","passwd","localhost","DBname");
$query = "SELECT * FROM user WHERE id = ' . $id";
$result = $dbhObj->execute($query);
while ($row = $result->fetch_assoc()) {
//do stuff with $row array
}