Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to define $mysqli once in a class?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
audiodef
Advocate
Advocate


Joined: 06 Jul 2005
Posts: 4952

PostPosted: Wed Jun 20, 2012 10:47 pm    Post subject: How to define $mysqli once in a class? Reply with quote

I have a class that goes like so:

Code:

class Radio{
   function mysqlConnect(){
      global $host,$user,$password,$database;

      $mysqli = new mysqli($host,$user,$password,$database);
      if (mysqli_connect_errno()) {
          printf("Connect failed: <p>%s</p>", mysqli_connect_error());
          exit();
      }
      
      return $mysqli;
   }
   function doSomething(){
      $mysqli=$this->mysqlConnect();
      ...
      $mysqli->close();
   }
   function doSomethingElse(){
      $mysqli=$this->mysqlConnect();
      ...
      $mysqli->close();
   }
   function doYetAnotherThing(){
      $mysqli=$this->mysqlConnect();
      ...
      $mysqli->close();
   }
}


I can't help but think there must be a more efficient way to do this - in other words, not have to define $mysqli and then close the connection inside each function. How would I do that?
_________________
Gentoo Studio: http://gentoostudio.org
Pappy's Kernel Seeds: http://kernel-seeds.gentoostudio.org
Linux 'Tude Tees: http://skreened.com/geektudetees

A cloud is evaporated water in the sky, thanks.
Back to top
View user's profile Send private message
Mad Merlin
Veteran
Veteran


Joined: 09 May 2005
Posts: 1134

PostPosted: Thu Jun 21, 2012 2:08 am    Post subject: Reply with quote

Use a constructor and class members...

Code:

class Radio
{
   private $mysqli;

   function __construct()
   {
      $this->mysqli = new mysqli($host, $user, $password, $database);
   }

   function something()
   {
      $this->mysqli->query(...);
      ...
   }

   ...
}


The constructor is called once when the object is instantiated, and the member variables are stored in the object.
_________________
Game! - Where the stick is mightier than the sword!
Back to top
View user's profile Send private message
audiodef
Advocate
Advocate


Joined: 06 Jul 2005
Posts: 4952

PostPosted: Thu Jun 21, 2012 1:14 pm    Post subject: Reply with quote

I thought it would have to do with a constructor, but I didn't know exactly how to implement it. Thanks!

I now notice a lot of these:

Code:

Warning: mysqli::close(): Couldn't fetch mysqli in /var/www/synthetronica/htdocs/radio/class_radio.php on line 112


Unless I remove $this->mysqli->close(); from the end of each function. Also, if the same function is called twice or more in one block, mysqli->close() at the end of the function seems to interfere. Where should mysqli->close() be placed?
_________________
Gentoo Studio: http://gentoostudio.org
Pappy's Kernel Seeds: http://kernel-seeds.gentoostudio.org
Linux 'Tude Tees: http://skreened.com/geektudetees

A cloud is evaporated water in the sky, thanks.
Back to top
View user's profile Send private message
Jeffrey0
n00b
n00b


Joined: 31 Dec 2004
Posts: 68

PostPosted: Fri Jun 22, 2012 2:50 am    Post subject: Reply with quote

audiodef wrote:
Also, if the same function is called twice or more in one block, mysqli->close() at the end of the function seems to interfere. Where should mysqli->close() be placed?

PHP automatically closes connections when the mysqli object is garbage collected, so there's no real need to close it manually with exception of a few very special cases.

Otherwise, you'd call it in your __destruct destructor. The destructor gets called when there's no references to your Radio instance left anymore.
Code:
class Demo {
  function __construct() {
    echo 'hi!';
  }
  function __destruct() {
    echo 'bye!';
  }
}

function make_a_demo() {
  $test = new Demo(); // hi!
}
make_a_demo();
// the $test from make_a_demo is gone now - bye! gets printed

$otherTest = new Demo(); // hi!
// end of the script - the destructor gets called here but it might be too late to actually send any text over to the browser
Back to top
View user's profile Send private message
audiodef
Advocate
Advocate


Joined: 06 Jul 2005
Posts: 4952

PostPosted: Fri Jun 22, 2012 3:14 pm    Post subject: Reply with quote

Thanks, I appreciate that.

I doubt I have any special cases requiring me to call close(), but what would those be?
_________________
Gentoo Studio: http://gentoostudio.org
Pappy's Kernel Seeds: http://kernel-seeds.gentoostudio.org
Linux 'Tude Tees: http://skreened.com/geektudetees

A cloud is evaporated water in the sky, thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum