I'm a new with OOP model in PHP. I has been trying to retrieve data from database but something related to private makes me stuck. This is my code.
<?php
require ("UserData.php");
class Database{
public function getUser($sql){
include ("includes/connect.php");
$statement = $conn->prepare ($sql);
$statement->execute();
while ($row = $statement->fetch()) {
$dataSet[] = new UserData($row);
}
if (!empty($dataSet)) {
return $dataSet;
}else{
die();
}
}
}
?>
the second file
<?php
class UserData
{
private $user_id, $phone,$name,$address;
public function _construct($dbrow){
$this->user_id = $dbrow['user_id'];
$this->name = $dbrow['name'];
$this->phone = $dbrow['phone'];
$this->address = $dbrow['address'];
}
public function getUserId(){
return $this->user_id;
}
public function getUserName(){
return $this->name;
}
public function getUserPhone(){
return $this->phone;
}
public function getUserAddress(){
return $this->address;
}
}
?>
and the last one
<?php require ("Database.php"); ?>
<html>
<head>
<title>OOP</title>
</head>
<body>
<?php
include("includes/connect.php");
$db = new Database();
$dataSet = $db -> getUser ("SELECT * from user");
if ($dataSet) {
foreach ($dataSet as $data) {
echo "<p>";
echo "ID" .$data->getUserId()."<br />";
echo "Name" .$data->getUserName()."<br />";
echo "Phone" .$data->getUserPhone()."<br />";
echo "Address" .$data->getUserAddress()."<br />";
echo "</p>";
}
}else{
echo "no result found";
}
?>
</body>
</html>
Well, I tried to var_dump
the dataSet
but the error shows up.
array(2) { [0]=> object(UserData)#5 (4) { ["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=> NULL ["name":"UserData":private]=> NULL ["address":"UserData":private]=> NULL } [1]=> object(UserData)#6 (4) { ["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=> NULL ["name":"UserData":private]=> NULL ["address":"UserData":private]=> NULL } }
So can anyone show me which spots make the code dump?