我想从类User转换一个Object,但是当我完成创建我的用户并尝试使用json_encode
转换它时,它会返回一个空的JSONObject - > {}
这是我的班级用户:
<?php
class User{
private $id;
private $nom;
private $prenom;
private $idImage;
private $identifiant;
private $email;
private $password;
private $dateNaissance;
private $dateInscription;
private $pays;
private $codePostal;
private $telephone;
public function __construct($id = null, $nom = "", $prenom = "", $idImage = null, $identifiant = "", $email = "",$password = "", $dateNaissance = "", $dateInscription = "", $pays = "", $codePostal = "", $telephone = "") {
$this->setId($id);
$this->setNom($nom);
$this->setPrenom($prenom);
$this->setIdImage($idImage);
$this->setIdentifiant($identifiant);
$this->setEmail($email);
$this->setPassword($password);
$this->setDateNaissance($dateNaissance);
$this->setDateInscription($dateInscription);
$this->setPays($pays);
$this->setCodePostal($codePostal);
$this->setTelephone($telephone);
}
public function setId($id){
$this->id = $id;
}
public function getNom() {
return $this->nom;
}
public function setNom($nom,$notify = false) {
$this->nom = $nom;
if($notify) $this->notifyObservers ();
}
public function getPrenom() {
return $this->prenom;
}
public function setPrenom($prenom,$notify = false) {
$this->prenom = $prenom;
if($notify) $this->notifyObservers ();
}
public function getIdImage() {
return $this->idImage;
}
public function setIdImage($imageProfile,$notify = false) {
$this->idImage = $imageProfile;
if($notify) $this->notifyObservers ();
}
public function getIdentifiant() {
return $this->identifiant;
}
public function setIdentifiant($identifiant,$notify = false) {
$this->identifiant = $identifiant;
if($notify) $this->notifyObservers ();
}
public function getEmail() {
return $this->email;
}
public function setEmail($email,$notify = false) {
$this->email = $email;
if($notify) $this->notifyObservers ();
}
public function getPassword() {
return $this->password;
}
public function setPassword($password,$notify = false) {
$this->password = $password;
if($notify) $this->notifyObservers ();
}
public function getDateNaissance() {
return $this->dateNaissance;
}
public function setDateNaissance($dateNaissance,$notify = false) {
$this->dateNaissance = $dateNaissance;
if($notify) $this->notifyObservers ();
}
public function getDateInscription() {
return $this->dateInscription;
}
public function setDateInscription($dateInscription,$notify = false) {
$this->dateInscription = $dateInscription;
if($notify) $this->notifyObservers ();
}
public function getPays() {
return $this->pays;
}
public function setPays($pays,$notify = false) {
$this->pays = $pays;
if($notify) $this->notifyObservers ();
}
public function getCodePostal() {
return $this->codePostal;
}
public function setCodePostal($codePostal,$notify = false) {
$this->codePostal = $codePostal;
if($notify) $this->notifyObservers ();
}
public function getTelephone() {
return $this->telephone;
}
public function setTelephone($telephone,$notify = false) {
$this->telephone = $telephone;
if($notify) $this->notifyObservers ();
}
public function __toString() {
return $this->nom . $this->password . $this->identifiant;
}
}
?>
这里我有我的测试文件:
<?php
require_once './UserDAO.class.php';
require_once './User.class.php';
$userDAO = new UserDAO();
$user = new User();
$json= json_encode($user);
echo $user;
echo $json;
?>
此外,我验证了用户是否真的被创建了,而且确实如此,所以我现在真的迷失了。谢谢你的关注!
答案 0 :(得分:4)
如果你想json_encode
一个对象(你使用的是PHP 5.4+),你可以实现JsonSerializable
。
<?php
class User implements \JsonSerializable
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function jsonSerialize()
{
return array(
'name' => $this->name,
);
}
}
$u = new User('here');
echo json_encode($u), PHP_EOL;
否则,您将无法使用公共属性。