haha, no I don't think I need to make objects to consider it good code. The main reasons are this.
- Once the initial coding is done, updating and scaling the features of the program will be easier.
- It's an administration site for a store. Users will eventually be able to search on a client, and get a list of all of the clients previous receipts/invoices. I can save the objects in a database and just recreate them later (although I'll save the variables separtately rather then the serialize method).
- I will be passing the objects to and fro around in my site, so it's easier to just create objects and store them in session variables.
- hehe ... I've never created objects in PHP before, so I want to do it this way just because it's fun. :rolleyes:
He is an example of what I'm talking about (sorry about the long post) ...
class InventoryItem {
var $mID;
var $mName;
//I took the rest out, but there are also sku,price,qty, errormessages, etc.
//Constructor:
function InventoryItem($ID) {
$this->mID = $ID;
$this->mName = "";
}
//Destructor : this is a hack, since PHP doesn't support destructors yet:
function Destroy() {
$this = 0;
}
function get_ID() {
return $this->mID;
}
function get_Name() {
return $this->mName;
}
function set_Name($Name) {
$this->mName = $Name;
return true;
}
}
class Invoice {
var $ItemsArray;
//Constructor:
function Invoice() {
$this->ItemsArray = array();
}
//Destructor : this is a hack, since PHP doesn't support destructors yet:
function Destroy() {
$this = 0;
}
function addInventoryItem($ID) {
$this->ItemsArray[] = new InventoryItem($ID);
}
function removeInventoryItem($ID) {
//took the code out to make this post smaller.
}
}else {
// Error handling needed here
}
}
function countInventoryItems() {
return count($this->ItemsArray);
}
}
Now, the problem is that I create and use the objects like this ..
$objInvoice = new Invoice();
$objInvoice->addInventoryItem(89);
$objInvoice->ItemsArray[0]->get_ID();
It would be much nicer to do something like this ..
$objInvoice = new Invoice();
$objInvoice->addInventoryItem(89);
$objInvoice->get_ID(0);
But in order to do that I would need to create a get_Id function in the inventory class, which would kind of be redundant since it would just call the get_ID function in the item class. Should I even care? Should I just leave it the way it is? I've never done anything like this before, so I just want to get the logic correct so I can do similar things in the future without finding out that this code isn't logical.
PS .. if you've managed to read this whole thing, thank you very much for the time .. 🙂