I have 2 classes that inherits from a father class. The “father” class has a protected variable that is being populated in both of the child classes, each of the classes populates it with different information for a database.
My problem is that when I create instances of both classes, the second instance changes the the values from the first instance.
Here is the code:
class User
{
//array with the MySQL Server details//
private $serverInfoArray = array("host" => HOST, "user" => USER, "pass" => PASS);
protected $data; //This will be populated with data in both of the "child" classes.
//some database connection functions....//
public function setData(){} //implemented on "child" classess
public function getData(){} //implemented on "child" classess
}
and here are one the child classes:
class Producer extends User
{
function __construct($id)
{
$this->setData($id);
}
public function setData($id)
{
global $serverInfoArray, $data;
$connectionFB;
$query = "MY FIRST QUERY"; //IN THE SECOND CHILD CLASS
//IT HAS DIFFERENT QUERY
$result = mysql_query($query, $connectionFB);
$i=0;
while($row = mysql_fetch_array($result))
{
$data[$i] = $row;
$i++;
}
}
public function getData()
{
global $data;
return $data;
}
}
The second class has the same structure but is different in its name and the query being executed. Its declaration is class Account extends User
When I create 2 instances:
$producer = new Producer($_SESSION['PRODUCER_ID']);
$account = new Account($_SESSION['ACCOUNT_ID']);
and do this:
var_dump($producer->getData());
I get the values of $data that was populated by the second query. If I wont create the account instance it will return the values of populated in the first class.
Why does the account instance changes the values of $data from the producer class instance, how can I separate them ?
Thanks.
Remove the following line:
Use
$this->serverInfoArray&$this->datato access class variables.