An example from my code of an abstract class and how to extend it to a concrete class. You really don't have to understand what's going on in the code; the purpose of this code is just simply to show you how to extend and utilize an abstract class.
The Abstract Class
The Concrete Class that extends that Abstract class
The Abstract Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* * | |
* AUTHOR/LICENSE HOLDER INFORMATION * | |
* @programmer Dino Cajic */ | |
chdir(__DIR__); require_once("../../../../classes/database/db_connect.php"); | |
chdir(__DIR__); require_once("../../../../classes/frequent/frequent.php"); | |
chdir(__DIR__); require_once("../../../../classes/error_reporting/error_reporting.php"); | |
chdir(__DIR__); require_once("../../frequent/narrow_down_navigation.php"); | |
/** | |
* Class AbstractViewIncomingTireShipments | |
* | |
* Contains common concrete methods and a few required abstract methods that need to be | |
* implemented by TireShipment_xx classes | |
*/ | |
abstract class AbstractTireShipments { | |
/** @var object DbConnect | Stores the DbConnect class instantiation */ | |
protected $_db_connect_obj; | |
/** @var resource $_mysqli | Stores the connection to the database */ | |
protected $_mysqli; | |
/** @var object Frequent | Stores the Frequent class instantiation */ | |
protected $_frequent_obj; | |
/** @var object ReportError | Stores the ReportError instantiation */ | |
protected $_error_reporting_obj; | |
/** @var object NarrowDownNavigation | Stores the NarrowDownNavigation instantiation */ | |
protected $_narrow_down_navigation_obj; | |
/** @var array $_tires_array | Stores the data that's retrieved through the sqlRetrieveContent() method */ | |
protected $_tires_array = array(); | |
/** | |
* Instantiates the classes | |
* Connects to the database | |
*/ | |
public function __construct() { | |
$this->_db_connect_obj = new DbConnect(); | |
$this->_mysqli = $this->_db_connect_obj->mysqli(); | |
$this->_frequent_obj = new Frequent(); | |
$this->_error_reporting_obj = new ReportError(); | |
$this->_narrow_down_navigation_obj = new NarrowDownNavigation(); | |
} | |
/** | |
* The main method that gets called. It will construct the page. | |
* | |
* @return void | |
*/ | |
public abstract function main(); | |
/** | |
* Retrieves the tire shipment information from the database | |
* | |
* @return void | |
*/ | |
protected abstract function sqlGetContent(); | |
/** | |
* Generates the SQL statement to be used in sqlGetContent() method | |
* | |
* @param string $sql | |
* @return string | |
*/ | |
protected function generateSQLstatement($sql){ | |
if (isset($_GET['type']) && !isset($_GET['location'])) { | |
$sql .= ' AND tires.id = orders.item_number_id'; | |
$sql .= ' AND orders.pack_slip = packing_slips.id'; | |
$sql .= ($_GET['type'] != 0) ? " AND packing_slips.status = '" . $_GET['type'] . "'": ''; | |
} | |
if (isset($_GET['location']) && !isset($_GET['type'])) { | |
$sql .= " AND tires.id = orders.item_number_id"; | |
$sql .= " AND orders.pack_slip = packing_slips.id"; | |
$sql .= " AND packing_slips.location = '" . $_GET['location'] . "'"; | |
} | |
if (isset($_GET['location']) && isset($_GET['type'])) { | |
$sql .= ' AND tires.id = orders.item_number_id'; | |
$sql .= ' AND orders.pack_slip = packing_slips.id'; | |
$sql .= ($_GET['type'] != 0) ? " AND packing_slips.status = '" . $_GET['type'] . "'": ''; | |
$sql .= " AND packing_slips.location = '" . $_GET['location'] . "'"; | |
} | |
$sql .= " ORDER BY packing_slips.date_arriving ASC"; | |
return $sql; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* * | |
* AUTHOR/LICENSE HOLDER INFORMATION * | |
* @programmer Dino Cajic */ | |
chdir(__DIR__); require_once("abstract/AbstractIncomingTireShipments.php"); | |
/** | |
* Class TireShipmentsItemNumberDateColumn | |
* | |
* Displays all of the incoming shipments grouped by part number and creates the columns to display | |
* @link index.php?page=views-tire_shipments_item_number_date_column&type=0 | |
*/ | |
class TireShipmentsItemNumberDateColumn extends AbstractTireShipments { | |
/** | |
* Generates the table to display incoming shipments grouped by part number | |
* | |
* @return void | |
*/ | |
public function main() { | |
if(!isset($_GET['type']) && !isset($_GET['location'])) { | |
$this->_frequent_obj->redirect(0, "?page=views-tire_shipments_item_number_date_column&type=0"); | |
return; | |
} | |
$data["name"] = "By Item Number"; | |
$data["page"] = "view_incoming_shipments"; | |
$this->_narrow_down_navigation_obj->main($data); // Extended | |
$this->sqlGetContent(); | |
?> | |
<div class="fluid-container head-set"> | |
<table class="table table-hover" id="sort"> | |
<thead> | |
<tr style="cursor:pointer;"> | |
<th>Item Number</th> | |
<th>Width</th> | |
<th>A/R</th> | |
<th>IC</th> | |
<th>Diam</th> | |
<th>LT?</th> | |
<th>Brand</th> | |
<th>Model</th> | |
<?php | |
$date_array = $this->generateMonthColumnNames(); | |
?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php $this->generateRows($date_array); ?> | |
</tbody> | |
</table> | |
</div> | |
<?php | |
} | |
/** | |
* Selects the order information and groups it by product number, month, location | |
* | |
* @return array | |
*/ | |
protected function sqlGetContent(){ | |
$sql = 'SELECT tires.item_number, tires.width, tires.aspect_ratio, tires.internal_construction, | |
tires.rim_diameter, tires.light_truck, brand.brand_name, model.model_name, | |
packing_slips.po_number, orders.qty, packing_slips.location, packing_slips.status, | |
packing_slips.date_arriving | |
FROM tires, brand, model, image, packing_slips, orders | |
WHERE tires.brand = brand.id | |
AND tires.model = model.id | |
AND tires.image = image.id | |
AND packing_slips.active = 1'; | |
$sql = $this->generateSQLstatement($sql); // Extended | |
if (!$stmt = $this->_mysqli->prepare($sql)) { | |
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $this->_mysqli->error; | |
$this->_error_reporting_obj->error($error); | |
die ($this->_mysqli->error); | |
} | |
if (!$stmt->execute()) { | |
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $stmt->error; | |
$this->_error_reporting_obj->error($error); | |
die ($stmt->error); | |
} | |
$data = array(); | |
if (!$stmt->bind_result( | |
$data["item_number"], | |
$data["width"], | |
$data["aspect_ratio"], | |
$data["internal_construction"], | |
$data["rim_diameter"], | |
$data["light_truck"], | |
$data["brand"], | |
$data["model"], | |
$data["po_number"], | |
$data["qty"], | |
$data["location"], | |
$data["status"], | |
$data["date_arriving"]) | |
){ | |
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $stmt->error; | |
$this->_error_reporting_obj->error($error); | |
die ($stmt->error); | |
} | |
while ($result = $stmt->fetch()) { | |
$item_number = htmlspecialchars($data["item_number"]); | |
$po_number = htmlspecialchars($data["po_number"]); | |
// _tires_array is extended | |
if (!array_key_exists($item_number, $this->_tires_array)) { | |
$this->_tires_array[$item_number] = array( | |
htmlspecialchars($data["width"]), | |
htmlspecialchars($data["aspect_ratio"]), | |
htmlspecialchars($data["internal_construction"]), | |
htmlspecialchars($data["rim_diameter"]), | |
htmlspecialchars($data["light_truck"]), | |
htmlspecialchars($data["brand"]), | |
htmlspecialchars($data["model"]) | |
); | |
} | |
$this->_tires_array[$item_number][$po_number] = array( | |
htmlspecialchars($data["qty"]), | |
htmlspecialchars($data["location"]), | |
htmlspecialchars($data["status"]), | |
htmlspecialchars($data["date_arriving"]) | |
); | |
} | |
$stmt->close(); | |
} | |
/** | |
* Cycles through the $tires_array parameter and generates column names | |
* | |
* @return array | |
*/ | |
private function generateMonthColumnNames(){ | |
$date_array = array(); | |
foreach($this->_tires_array as $key => $value) { | |
foreach($value as $newKey => $newValue) { | |
if (is_int($newKey)) { | |
continue; | |
} | |
$date_column = date("F Y", strtotime($newValue[3])); | |
if (!in_array($date_column, $date_array)) { | |
echo "<th>" . $date_column . "</th>"; | |
array_push($date_array, $date_column); | |
} | |
} | |
} | |
return $date_array; | |
} | |
/** | |
* Generates the rows | |
* | |
* @param $date_array | |
* @return void | |
*/ | |
private function generateRows($date_array){ | |
$date_array = array_flip($date_array); | |
foreach($this->_tires_array as $key => $value) { | |
echo "<tr>". | |
"<td>$key</td>" . # Item Number | |
"<td>$value[0]</td>". # Width | |
"<td>$value[1]</td>". # AR | |
"<td>$value[2]</td>". # I/C | |
"<td>$value[3]</td>". # Diam | |
"<td>$value[4]</td>". # LT | |
"<td>$value[5]</td>". # Brand | |
"<td>$value[6]</td>"; # Model | |
for ($i=0; $i<=7; $i++) { | |
unset($value[$i]); | |
} | |
for ($i=0; $i<=end($date_array); $i++) { | |
echo '<td>'; | |
foreach($value as $newKey => $newValue) { | |
$date_column = date("F Y", strtotime($newValue[3])); | |
if (array_key_exists($date_column, $date_array) && $i == $date_array[$date_column]) { | |
$location = $this->_frequent_obj->generateLocationName($newValue[1]); | |
$status = $this->_frequent_obj->generateStatusName($newValue[2]); | |
echo '<span style="cursor:pointer;" title="' . $newValue[3] . '">' . sprintf("%02d", $newValue[0]) . '</span>'; | |
echo ' <span style="cursor:pointer;" title="' . $location . '">(' . substr($location, 0, 1) . ')</span>'; | |
echo ' <span style="cursor:pointer;" title="' . $status . '">(' . substr($status, 0, 1) . ')</span><br />'; | |
} | |
} | |
echo '</td>'; | |
} | |
echo "</tr>"; | |
} | |
} | |
/** | |
* Closes the connection | |
*/ | |
public function __destruct() { | |
$this->_mysqli->close(); | |
} | |
} |
Comments
Post a Comment