Click to See Complete Forum and Search --> : need some class


reddrum
08-14-2007, 02:33 PM
As a former VB6 programmer I rarely used classes, so I am not only new to OOP this is my first in PHP. I appreciate any input.
Thanks!

class jobs{
var $quote, $date, $qty, $price, $papid, $lth, $wth;
var $ink_ft = array();
var $ink_bk = array();
var $papName, $papType, $papWeight, $papColor;

function jobs($qt_no, $qty_index){
$this->quote = $qt_no;
$price_index = "fldPrice".$qty_index;
$qty_index = "fldQty".$qty_index;
$strSQL = "select fldDate, $qty_index, $price_index, fldGroupID, fldLth, fldWth,
fldInkFt1, fldInkFt2, fldInkFt3, fldInkFt4, fldInkFt5,
fldInkBk1, fldInkBk2, fldInkBk3, fldInkBk4, fldInkBk5
from tblQuotes where fldQtID = {$this->quote}";

$result = mysql_query($strSQL);
if ($result){
$row = mysql_fetch_assoc($result);
$this->date = $row['fldDate'];
$this->qty = $row[$qty_index];
$this->price = $row[$price_index];
$this->papid = $row['fldGroupID'];
$this->lth = $row['fldLth'];
$this->wth = $row['fldWth'];
for($i=1; $i<6; ++$i){
$strTmp = "fldInkFt".$i;
if($row[$strTmp] != '\0' || $row[$strTmp] != "")
$this->ink_ft[$i] = $row[$strTmp];
$strTmp = "fldInkBk".$i;
if($row[$strTmp] != '\0' || $row[$strTmp] != "")
$this->ink_bk[$i] = $row[$strTmp];
}
mysql_free_result($result);
}
else{
echo mysql_error();
}
}
function paper($papid){
$strSQL = "SELECT fldName, fldType, fldWeight, fldColor FROM
tblPaper WHERE fldGroup = $papid AND fldFirstRec = 1";
$result = mysql_query($strSQL);
if ($result){
$row = mysql_fetch_assoc($result);
$this->papName = $row['fldName'];
$this->papType = $row['fldType'];
$this->papWeight = $row['fldWeight'];
$this->papColor = $row['fldColor'];
}
else{
echo mysql_error();
}
}
}

function view_jobs(){
$memid = $_POST['member'];
$obj = array();
$job_id = array();
$qty = array();
$quote = array();
$i=0;

$strSQL = "SELECT tblJobs.fldJobID, tblJobs.fldQuoteNo,
fldQty FROM tblJobs WHERE tblJobs.fldMember = $memid";

mysql_connect("xxxxx", "xxxxx", "xxxxx");
mysql_select_db("xxxxxx");

$result = mysql_query($strSQL);
if($result){
//show all open jobs
while ($row = mysql_fetch_array ($result)){
$job_id[$i] = $row['fldJobID'];
$qty[$i] = ($row['fldQty'] + 1);
$quote[$i] = $row['fldQuoteNo'];
$obj[$quote[$i]] = new jobs($quote[$i], $qty[$i]);
$obj[$quote[$i]]->paper($obj[$quote[$i]]->papid);
print"{$obj[$quote[$i]]->date} {$obj[$quote[$i]]->qty} {$obj[$quote[$i]]->price}<br />";
print("front:&nbsp;\n");
foreach($obj[$quote[$i]]->ink_ft as $inkft){
print("$inkft\n");
}
print("<br />\n");
print("back:\n");
foreach($obj[$quote[$i]]->ink_bk as $inkbk){
print("$inkbk\n");
}
print("<br />\n");
print"Paper:&nbsp;{$obj[$quote[$i]]->papName} {$obj[$quote[$i]]->papType}
{$obj[$quote[$i]]->papWeight} {$obj[$quote[$i]]->papColor}<br />";
print("<br /><br />\n");
++$i;
}


mysql_free_result($result);

//destroy $obj
foreach($obj as $koObj){
unset($koObj);
}
//see if $obj is destroyed
print"test {$obj[$quote[$i]]->date}<br />";
}
else{
echo mysql_error();
}
mysql_close();
}

Shrike
08-14-2007, 04:46 PM
A few quick observations:

The class does way too much. One key goal in object oriented software is separation of concerns. Database access, request handling and presentation are a common separation (see Model-View-Controller).

Having all things lumped into the one often makes a class hard to understand. The simpler something is, the better, right? Well designed objects actually do very little themselves, but rather it's the way they interact that produces interesting things (see "The God class").

The method Jobs::view_jobs() seems to create many instances of Jobs. Perhaps this is a case for abstracting view_jobs() into it's own class.

reddrum
08-14-2007, 06:36 PM
Yes I agree the class is very busy, I could have accomplished the same (far simpler) outcome using my old procedural based ways, the reason I have avoided object oriented is because I have trouble seeing the advantage. The reason I am trying to learn OOP now is, I want to use something like a UDT with PHP. Until I fully understand the new ways, I think Ill just concentrate on making classes that look more like a UDT. And where to see "Model-View-Controller, The God class".
Thanks for the good advise!