831 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
tigerlabs says

Hey guys,

I’m trying to use __construct() to connect, then __destruct() to disconnect from MySQL.

However when I try to, I get 2 errors:

Undefined variable: mysql in C:\wamp\www\kjv\classes\KJV.class.php on line 16

and

Fatal error: Call to a member function close() on a non-object in C:\wamp\www\kjv\classes\KJV.class.php on line 16
Here is my class file code..

class KJV {

    /* Database Variables */
    private $host = "localhost";
    private $dbuser = "username";
    private $dbpass = "password";
    private $dbname = "kjv";

    function __construct() {
    $mysql = new mysqli($this->host, $this->dbuser, $this->dbpass, $this->dbname);
    if (mysqli_connect_errno()) { echo "Can't connect to MySQL Server. Error Code: " . die(mysqli_connect_error()); } 
    }
    function __destruct() {
        $mysql->close();
    }

// End class
}

I’m sure I’m making some noob mistake, but I’m just learning OOP . :)

Thanks in advance.

499 posts
  • Canada
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
TutelageSystems says

When you refer to a variable like you are there, you are referring to it “locally”. It is saying it is undefined because __construct() defines the variable $mysql, but once it leaves that function the variable $mysql is “garbage” as it was created within that function and not in the class.

You need to change it to a class member.

class KJV {

    /* Database Variables */
    private $host = "localhost";
    private $dbuser = "username";
    private $dbpass = "password";
    private $dbname = "kjv";
    private $mysql;

    function __construct() {
    $this->mysql = new mysqli($this->host, $this->dbuser, $this->dbpass, $this->dbname);
    if (mysqli_connect_errno()) { echo "Can't connect to MySQL Server. Error Code: " . die(mysqli_connect_error()); } 
    }
    function __destruct() {
        $this->mysql->close();
    }

// End class
}
831 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
tigerlabs says

Awesome, it worked!

Thanks man.

I really like OOP , it is going to be a bit difficult to get used to but it’s very neat.

499 posts
  • Canada
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
TutelageSystems says

Just remember the difference between a “class level” variable and “local”.

class foo
{
  var name;

  function foo()
  {
    $this->name = 'Mickey';
  }

  function setName($name)
  {
    // different between the name variables.
    $this->name = $name;
  }

  function printName()
  {
    print $this->name;
  }
}
<\pre>
by
by
by
by
by
by