Skip to main content

Abstract Class Example

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
<?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;
}
}
The Concrete Class that extends that Abstract class
<?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

Popular posts from this blog

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition...

Creating your own ArrayList in Java

Wanted to show that certain data structures in Java can be created by you. In this example, we'll go ahead and create an ArrayList data structure that has some of the methods that the built in ArrayList class has. We'll create 2 constructors: The default constructor that creates an ArrayList with a default size of 10. Constructor that allows an initial size to be passed to the array. We'll also create a number of methods: void add(Object x);  A method that allows you to place an Object at the end of the ArrayList. void add(int index, Object x);  A method that allows you to place a value at a given location. Object get(int index):  Allows you to retrieve a value of the arrayList array from a given location. int size();  Allows you to get the number of elements currently in the Arraylist. boolean isEmpty();  Tests to see if the Arraylist is empty. boolean isIn(Object x);  A method that sees if a particular object exist in the arrayList. int ...

Laravel 6.x with React and react-router

This will get you started on getting your first React/Laravel application deployed to your server. We'll cover everything from installation to deployment. Start by reading the installation instructions on  https://laravel.com/docs/6.x#installing-laravel . We'll cover those details below. Setting Up Laravel Check that you have the latest version of PHP installed on your computer.  It must be >= 7.2.0. Open terminal to get the Laravel installation tool. Type in composer global require laravel/installer Type in laravel to verify installation. Navigate to a directory on your computer where you want to install your project on your terminal. Run the following command: laravel new project_name (replace project_name with your project name). Once complete, cd into your new project. Type the following command: php artisan serve. You'll get a message like the following if it's running successfully: Laravel development server started: http://127.0.0.1:8000 ...