Hey authors, I need your help!
I am creating my own PHP5 application and I figure out that my objects of type 'User' don't return their fields of table using method toString. Why is it happen? Why? The server can create objects, in this case, 4 objects of type 'User' but can't return your data.
You are free copy the next code and test in your server.
Class PropertyObject
Code:
<?php
require_once('interface.Validator.php');
abstract class PropertyObject implements Validator{
protected $propertyTable = array();
protected $changedProperties = array();
protected $data;
protected $errors = array();
public function __construct($arData){
$this->data = $arData;
}
function __get($propertyName){
if(!array_key_exists($propertyName,$this->propertyTable))
throw new Exception("Invalid property \"$propertyName\"!");
if(method_exists($this,'get'.$propertyName)){
return call_user_func(array($this,'get'.$propertyName));
}else{
return $this->data[$this->propertyTable[$propertyName]];
}
}
function __set($propertyName,$value){
if(!array_key_exists($propertyName,$this->propertyTable))
throw new Exception("Invalid property \"$propertyName\"!");
if(method_exists($this,'set'.$propertyName)){
return call_user_func(array($this,'set'.$propertyName),$value);
}else{
if($this->propertyTable[$propertyName] != $value && !in_array($propertyName,$this->changedProperties)){
$this->changedProperties[]=$propertyName;
}
$this->data[$this->propertyTable[$propertyName]] = $value;
}
}
function validate(){
}
}
?>
Class User
Code:
<?php
require_once('class.PropertyObject.php');
class User extends PropertyObject{
function __construct($userid){
$arData = DataManager::getUserData($userid);
parent::__construct($arData);
$this->propertyTable['userid']='userid';
$this->propertyTable['id']='userid';
$this->propertyTable['email']='semail';
$this->propertyTable['password']='spassword';
}
function validate(){
if(strlen($this->password)<4){
$this->errors['password'] = 'O tamanho da password deve ser superior a 4 caracteres!';
}
if(!filter_var($this->email, FILTER_VALIDATE_EMAIL)){
$this->errors['email'] = 'Este $this->email não é válido!';
}
if(sizeof($this->errors)){
return false;
}else{
return true;
}
}
function __toString(){
return $this->id.' '.$this->email.' '.$this->password;
}
}
?>
Class DataManager
Code:
<?php
require_once('class.User.php');
class DataManager{
private static function _getConnection(){
static $hDB;
if(isset($hDB)){
return $hDB;
}
$hDB = new mysqli('hostname','username','password','dbname');
if(mysqli_connect_error()){
die('Failure connecting to the database! Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
//debug
//echo 'Success... ' . mysqli_get_host_info($hDB) . "\n";
return $hDB;
}
public static function getAllUsersAsObjects(){
$sql = "SELECT * FROM user;";
$res = mysqli_query(DataManager::_getConnection(),$sql);
if(!$res){
die("Falhou ao obter todos utilizadores da aplicação");
}
if(mysqli_num_rows($res)){
$objs=array();
while($row = mysqli_fetch_assoc($res)){
$objs[] = new User($row['id']);
}
return $objs;
}else{
return array();
}
}
public static function getUserData($userid){
$sql = "SELECT * FROM user WHERE id='".$userid."';";
$res = mysqli_query(DataManager::_getConnection(),$sql);
if(!($res && mysqli_num_rows($res))){
die("Falhou ao obter dados sobre a data do utilizador $userid!");
}
return mysqli_fetch_assoc($res);
}
}
?>
test.php
Code:
<?php
require_once('class.DataManager.php');
function println($data){
print $data."<br>\n";
}
$arUsers = DataManager::getAllUsersAsObjects();
/*echo "<pre>";
var_dump($arUsers);
echo "</pre>";*/
foreach($arUsers as $objEntity){
if(get_class($objEntity) == 'User'){
print "<h1>User - {$objEntity->__toString()}</h1>";
}
}
?>
SQL
Code:
create table user(
id int auto_increment,
email varchar(255) not null default '',
password varchar(255) not null default '',
primary key(id)
)engine=innodb;
insert into user(email,password) values ("[email protected]" ,"user1"),("[email protected]","user2"),("[email protected]","user3"),("[email protected]","user4");