Communicating Between Classes
Posted: Thu Mar 13, 2008 11:10 pm
Hi. I'm currently writing a small CMS (sort of) for personal use.
I'm trying to write it with OOP but it's been nothing but headaches. I'm trying to do a few things and I can't find much info on how to do them.
A) A global class that deals with the database (the connection, queries, etc)
B) Another global class that deals with errors (Handles them, log, etc)
C) Use methods from one object in another object (IE use the database object in another class like "user" to get a users information).
Is there any way to use the database object (And an error handling object that I haven't wrote yet) without passing them to every object I want to use them in.
Here is my working code. If anyone can point me towards a more elegant way to do this, I would appreciate it.
And 'tiff_func.php' just contains this function.
I'm trying to write it with OOP but it's been nothing but headaches. I'm trying to do a few things and I can't find much info on how to do them.
A) A global class that deals with the database (the connection, queries, etc)
B) Another global class that deals with errors (Handles them, log, etc)
C) Use methods from one object in another object (IE use the database object in another class like "user" to get a users information).
Is there any way to use the database object (And an error handling object that I haven't wrote yet) without passing them to every object I want to use them in.
Here is my working code. If anyone can point me towards a more elegant way to do this, I would appreciate it.
Code: Select all
<?php
require_once('tiff_func.php');
class database
{
private static $db_handle;
function __construct()
{
require_once('config/db_conf.php');
$this->db_handle = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS)
or exit(mysql_error());
mysql_select_db(MYSQL_DB)
or exit(mysql_error());
}
function query($sql)
{
$sql = mysql_real_escape_string($sql);
echo $sql; //debug purposes.
if (empty($sql))
{
return NULL; //NO query given.
}
else
{
$results = mysql_query($sql);
}
if ($results === TRUE)
{
return 1;
}
if ($results === FALSE)
{
return 0;
}
if (mysql_num_rows($results) == 1 && mysql_num_fields($results) == 1)
{
$results = mysql_fetch_row($results);
return $results[0];
}
while ($tmp = mysql_fetch_assoc($results))
{
$resultArr[] = $tmp;
}
if (is_array($resultArr[0]) && count($resultArr) == 1)
{
return $resultArr[0];
}
return $resultArr;
}
function result_count($table, $row = NULL, $match1 = NULL, $match2 = NULL)
{
if (empty($row))
{
$row = '*';
}
$sql = "SELECT count($row) FROM $table";
if (!empty($match1) && empty($match2))
{
$sql .= " WHERE $row = '$match1'";
}
if (!emptyEx($match1, $match2))
{
$sql .= " WHERE $match1 = '$match2'";
}
$results = $this->query($sql);
return $results;
}
function __destruct()
{
mysql_close($this->db_handle);
}
}
$db_handle = new database();
class input_validator {
function user_name($user_name) {
$pattern = "/^[a-zA-Z0-9]*$/";
$user_name = htmlspecialchars($user_name);
if (!preg_match($pattern, $user_name))
{
//ERROR
return 0;
}
return 1;
}
}
$input_validator = new input_validator();
class user {
public $current_user;
function __construct($db_handle)
{
$this->db_handle = $db_handle;
}
function return_all_users()
{
$users = $this->db_handle->query("SELECT * FROM userinfo");
if(empty($users))
{
$users = "No users";
}
return $users;
}
}
$user_handle = new user($db_handle);
//print all the users in the database.
$users = $user_handle->return_all_users();
print_r($users);
//check to see if a username is valid (in this case, only alpha numeric)
$user_names = array("ryan", "ry@n");
foreach ($user_names as $cur_name)
{
echo "$cur_name is ";
if ($input_validator->user_name($cur_name))
{
echo "Valid Username <br />";
}
else
{
echo "Invalid Username <br />";
}
}
?>
Code: Select all
function emptyEx()
{
if (!(func_num_args() > 0))
{
return NULL;
}
$args = func_get_args();
foreach ($args as $tmp)
{
if (empty($tmp))
{
return 1;
}
}
return 0;
}