Re: Starting OOP
From: Mike Smith (msmith_at_larrymethvin.com)
Date: 12/16/03
- Next message: Matthew Vos: "Re: [PHP] Re: Opening large file problem - fopen"
- Previous message: Alfredo: "Re: [PHP] trouble parsing an XML document"
- In reply to: Daniel Pupius: "Re: Starting OOP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: php-general@lists.php.net, Daniel Pupius <dan.pupius@actelearning.com> Date: Tue, 16 Dec 2003 12:23:31 -0800
Daniel Pupius wrote:
> It would be a good idea to create a database abstaction class. This means
> that should you move your application to a different database you only have
> to replace your database class, instead of recoding everything with the new
> set of functions. It can also be neater than using the mysql functions
> directly.
>
> You could use it as in a composite or an aggregated relationship. Check out
> http://www.phppatterns.com/index.php/article/articleview/15/1/1/ for some
> useful information.
>
> Also is it necessary to have $ponum sent to each function, how about
> creating an attrbute of the class and setting it in the constructor
> function.
>
>
>
> "Mike Smith" <msmith@larrymethvin.com> wrote in message
> news:20031215232145.57946.qmail@pb1.pair.com...
>
>>I've been doing procedural coding for a few month's, but perceive the
>>need for OOP for some of the projects I've done. I'm starting out and
>>and would like some feedback, before I tread down the wrong path. The
>>books I'm looking at Professional PHP (Wrox), and Visual QuickStart PHP
>>have examples, but I seem to need something more concrete so I've
>>started a skeleton to rewrite a Purchase Order application. Here is the
>>layout:
>>
>><?php //pocode.php
>>class PurchaseOrder
>>{
>>//Variables
>>var $ponum; //This will be a $_SESSION variable if that matters
>>var $item;
>>
>>function AddItem($ponum,$item){
>>...INSERT SQL CODE
>>}
>>
>>function DeleteItem($ponum,$item){
>>...DELETE SQL CODE
>>}
>>
>>function UpdateItem($ponum,$item){
>>...UPDATE SQL CODE
>>}
>>
>>function ListItems($ponum){
>>...SELECT SQL CODE, RETURN RESULTS (ie.
>>while($row=mssql_fetch_array($rst)){echo $row[0]....})
>>}
>>
>>}
>>?>
>>
>>I would then include that in my page,
>>and add items to a PO by doing something like
>><?php
>>include('pocode.php');
>>$po = new PO();
>>
>>If($_POST['submit']=='Add'){
>>//A HTML Button next to a text box to add a line item
>>$po->AddItem('12345','5063');
>>}
>>
>>$po->ListItems('12345');
>>
>>?>
>>
>>Do I have the basic concept right? This is like relearning to code.
>>Perhaps I learned in a bad way if that's the case. Any especially good
>>tutorials or books to recommend?
>>
>>Thanks
Thank you for the response. Based on your and Martin's responses I have
something like this:
<?php
class PurchaseOrder
{
//Variables
var $ponum;
var $item;
var $qty;
function PurchaseOrder(){
$this->ponum = $_SESSION['ponum'];
$this->item = $_POST['item'];
$this->qty = $_POST['qty'];
$this->price = $_POST['price'];
}
function AddItem(){
$sql = "INSERT INTO podtl (item, qty, price) VALUES
('$this->item',$this->qty,$this->price)";
}
function DeleteItem($itemid){
$sql = "DELETE FROM podtl WHERE id=$itemid";
}
function UpdateItem($itemid){
$sql = "UPDATE podtl SET price=$this->price, item='$this->item' WHERE
id=$itemid";
}
function ListItems(){
$sql = "SELECT id, item, qty, price FROM podtl WHERE ponum='$this->ponum'";
}
}
?>
As far a databsae independance. ADODB looks pretty thorough
(http://php.weblogs.com/ADODB). It seems several people tend to go the
PEAR route. Is that more of a preference or are there pseudo-religious
feelings on database abstraction?
Thanks for the response.
- Next message: Matthew Vos: "Re: [PHP] Re: Opening large file problem - fopen"
- Previous message: Alfredo: "Re: [PHP] trouble parsing an XML document"
- In reply to: Daniel Pupius: "Re: Starting OOP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]