I'll try to use fetch_object('Classname') to test this method.
The Class Inserent in Inserent.php:
namespace classes\model;
class Inserent{
private $nummer;
private $nickname;
private $email;
public function __construct ($nummer, $nickname, $email){
$this->nummer = $nummer;
$this->nickname = $nickname;
$this->email = $email;
}
public function getNummer(){
return $this->nummer;
}
public function setNummer($nummer){
$this->nummer = $nummer;
}
...
}
The use is in a Mapper-Class InserentDAO.php:
namespace classes\mapper;
use classes\model\Inserent;
class InserentDAO{
private $dbConnect;
public function __construct (){
$this->dbConnect = MySQLDatabase::getInstance();
}
public function readAll(){
$sql = "SELECT inserentennummer, nickname, email FROM inserent";
$inserent = null;
$inserentList = array();
if ($result = $this->dbConnect->query($sql)) {
while ($inserent = $result->fetch_object('classes\model\Inserent')) {
$inserentList[] = $inserent;
}
$result->close();
}
return $inserentList;
}
...
}
I get errors and my Objects are empty:
Warning: Missing argument 1 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10
Warning: Missing argument 2 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10
Warning: Missing argument 3 for classes\model\Inserent::__construct() in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 10
Notice: Undefined variable: nummer in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 11
Notice: Undefined variable: nickname in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 12
Notice: Undefined variable: email in D:\xampp\htdocs\workspace\secondStep\classes\model\Inserent.php on line 13
But if I change the code, in the Mapper, it will works.
part of the code InserentDAO.php instead:
while ($obj = $result->fetch_object()) {
$inserent = new Inserent($obj->inserentennummer, $obj->nickname, $obj->email);
$inserentList[] = $inserent;
}
I believe fetch_object
invokes an empty constructor and then sets the properties according to result columns it fetched. There is a comment on the manual confirming that.
So this is what fetch_object("classes\model\Inserent")
is doing:
$object = new classes\model\Inserent();
$object->inserentennummer = $result['inserentennumer'];
$object->nickname = $result['nickname'];
$object->email = $result['email'];
return $object;
This is why it's complaining about missing parameters and undefined variables (they're private
).
The obvious solution is to refactor your class to provide an empty constructor and public properties:
namespace classes\model;
class Inserent{
public $nummer;
public $nickname;
public $email;
public function __construct ($nummer = 0, $nickname = "", $email = ""){
//...
An alternative to making the properties public would be to use the __set
magic method, but you'd still have to provide a zero-parameter constructor.
Thank you for your answer!!
I had to change the constructor in my Class Inserenten.php in the way that you explain. Also I had to change the property name from "$nummer" to "$inserentennummer", because the database table property has this name. Also I had to add querys to the constructor.
The method __set() was no help.
class Inserent{
private $inserentennummer;
private $nickname;
private $email;
public function __construct ($inserentennummer = 0, $nickname = "", $email = ""){
if(!$this->inserentennummer){
$this->inserentennummer = $inserentennummer;
}
if(!$this->nickname){
$this->nickname = $nickname;
}
if(!$this->email){
$this->email = $email;
}
}
And then it's working :-)