SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    aggregating class with database object

    I have been over this issue before, and I know there is much written about it, but I'm having some difficulty implementing a solution.

    I want to aggregate an existing class with a database object. I'm having difficulty passing the object around, however. The method init() below is called by the constructor and sets the database conection. The trouble is that none of my other methods can properly access the object. What am I doing wrong?

    PHP Code:
    function init ( &$invoice_id )
            {
                
    $this->items         = array();
                
    $this->total         0;
                
    $this->grandtotal     0;
                
    $this->invoice_id     $invoice_id;
             
    $this->db         = &$this->getDBConn();
          
    $this->removeall();    /* remove all products from db associated with current invoice_id */
            
    }
            
            function 
    getDBConn ()
            {
                
    $db = &new MySQL DBHOSTDBUSERDBPASSDBNAME );
                return 
    $db;
            }
        
            function 
    add()
            {
                 
    $sql "insert into $table....";
                 
    $this->db->query$sql );
                  
    etc....
            } 
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by garlinto
    I have been over this issue before, and I know there is much written about it, but I'm having some difficulty implementing a solution.

    I want to aggregate an existing class with a database object. I'm having difficulty passing the object around, however. The method init() below is called by the constructor and sets the database conection. The trouble is that none of my other methods can properly access the object. What am I doing wrong?

    PHP Code:
    function init ( &$invoice_id )
             {
                 
    $this->items         = array();
                 
    $this->total         0;
                 
    $this->grandtotal     0;
                 
    $this->invoice_id     $invoice_id;
             
    $this->db         = &$this->getDBConn();
          
    $this->removeall();    /* remove all products from db associated with current invoice_id */
             
    }
             
             function 
    getDBConn ()
             {
                 
    $db = &new MySQL DBHOSTDBUSERDBPASSDBNAME );
                 return 
    $db;
             }
         
             function 
    add()
             {
                  
    $sql "insert into $table....";
                  
    $this->db->query$sql );
                   
    etc....
             } 
    Change the beginning of the getDBConn() function definition to this:

    function &getDBConn ()
    { // .....

    If it still doesn't work do a var_dump($this->db) in init() right after the assignment to $this->db.

    --ed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •