I have a difficulty understanding this,
The values for $dbhost $dbuser; $dbpass; $dbname; $dbconnection; are already declared directly under the class name, so it is expected to be accessed in the entire class.
class Database {
protected $dbhost;
protected $dbuser;
protected $dbpass;
protected $dbname;
protected $dbconnection;
}
In constructor(__construct), the values were initialized using the $this->[variablename], hence:
$dbhost = $this->dbhost = 'localhost';
$dbuser = $this->dbuser = 'root';
$dbpass = $this->dbpass = '';
$dbname = $this->dbname = 'forms_db';
$dbconnection = $this->dbconnection = (mysql_connect($dbhost, $dbuser, $dbpass));
the new values are expected to be stored in the class variables as their values have been changed
however, whenever I access the values using another method, I get an Undefined variable errors.
here is the rest of my code:
<?php
class Database {
protected $dbhost;
protected $dbuser;
protected $dbpass;
protected $dbname;
protected $dbconnection;
function __construct() {
$dbhost = $this->dbhost = 'localhost';
$dbuser = $this->dbuser = 'root';
$dbpass = $this->dbpass = '';
$dbname = $this->dbname = 'forms_db';
$dbconnection = $this->dbconnection = (mysql_connect($dbhost, $dbuser, $dbpass));
if(!$dbconnection) die("Could not connect to database. " . mysql_error());
return ($dbconnection);
}
function testing(){
echo $dbhost;
echo $dbuser;
echo $dbpass;
echo $dbname;
echo $dbconnection;
}
}
$data = new Database();
$data->testing();