无法将简单对象转换为 JSONObject


Can't convert a simple object to JSONObject

我想从类 User 转换一个对象,但是当我完成创建我的用户并尝试使用 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; 
?>

另外,我验证了用户是否真的被创建,确实如此,所以我现在真的很迷茫。所以感谢您的关注!

如果你想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;

否则,您将坚持使用公共属性。