| View previous topic :: View next topic |
| Author |
Message |
audiodef Advocate


Joined: 06 Jul 2005 Posts: 4952
|
Posted: Wed Jun 20, 2012 10:47 pm Post subject: How to define $mysqli once in a class? |
|
|
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 |
|
 |
Mad Merlin Veteran

Joined: 09 May 2005 Posts: 1134
|
Posted: Thu Jun 21, 2012 2:08 am Post subject: |
|
|
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 |
|
 |
audiodef Advocate


Joined: 06 Jul 2005 Posts: 4952
|
Posted: Thu Jun 21, 2012 1:14 pm Post subject: |
|
|
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 |
|
 |
Jeffrey0 n00b

Joined: 31 Dec 2004 Posts: 68
|
Posted: Fri Jun 22, 2012 2:50 am Post subject: |
|
|
| 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 |
|
 |
audiodef Advocate


Joined: 06 Jul 2005 Posts: 4952
|
|
| Back to top |
|
 |
|