Well your idea is similar to "C", just worded differently.
What makes a good class? A strong job or role.
I think that is an excellent way to describe the purpose of a class, and in this case I feel that the link class would just be data storage, and not worth a class.
Thanks to everyone, this has helped me understand when to apply OO Programming to a situation and helped me define what makes a class worth creating.
Since we are on the topic of classes, I figured I'd ask for some feedback on my database class, which I wrote yesterday(does it meet the suggested requirements for a class, is it well written?):
PHP Code:
<?php
//database.class.php
//Property of Greg (Selatos)
//Unauthorized use, distribution, and copying is prohibited
//make sure the template is included or the die() wrapper statements will cause an error
require_once("template.class.php");
//the database class, one instance is used for all the script and is used as a
//wrapper for all the query classes etc. Handles multiple queries via queryIDs
//some definitions to save space
define("DB_CONNECT_FAILURE", "A connection to the database was unable to be established.
Please refresh this page to try again.");
define ("DB_GONE_MISSING", "The Database that was selected could not be found. Please refresh to try again.");
class Database {
var $query; //array of Query objects
function Database($user, $pass, $database, $host = "localhost") {
$dbcnct = @mysql_connect($host, $user, $pass) or $template->panic(DB_CONNECT_FAILURE);
$db = @mysql_select_db($database, $dbcnct) or $template->panic(DB_GONE_MISSING);
}
//create a query
//the $sql statement should have %p in all of the positions where a parameter
//will be placed. add the parameters to the array in the order they will be
//placed in the query
function createQuery($sql, $parameters) { //$parameters is an array
$queryID = count($this->query);
$this->query[$queryID] = array("query" => new Query($sql, $parameters), "position" => 0);
return $queryID;
}
//iterator for queries
function iterate($queryID = -1) { //default value is the ID of the last created query
if ($queryID == -1) {
$queryID = count($this->query)-1;
}
return $this->query[$queryID]["query"]->getResult($this->query[$queryID]["position"]++);
}
//change database
function changeDatabase($database) {
@mysql_select_db($database) or $template->panic(DB_GONE_MISSING);
}
//function to free memory and release a query
function releaseQuery($queryID = -1) { //default value is the ID of the last created query
if ($queryID == -1) {
$queryID = count($this->query)-1;
}
unset($this->query[$queryID]);
}
}
//create the query class
class Query {
var $sql;
var $resultset;
function Query($sql, $parameters) {
$count = 0;
//cut up the string so that ereg_replace doesnt replace all the symbols
$sqlA = explode("%p", $sql);
$sql = $sqlA[0];
while ($count < count($parameters)) {
//make the parameter safe
$parameters[$count] = mysql_escape_string($parameters[$count]);
//add to query
$sql .= '"'.$parameters[$count].'"'.$sqlA[$count+1];
$count++;
}
//execute query
$this->sql = $sql;
$result = mysql_query($sql);
$this->resultset = array();
$count = 0;
while ($temp = mysql_fetch_array($result)) {
$this->resultset[$count] = $temp;
$count++;
}
}
//function to show the query(debugging purposes)
function getQuery() {
return $this->sql;
}
//function to return the next array in the resultset
function getResult($pos) {
return $this->resultset[$pos];
}
}
?>
Bookmarks