1

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

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

4

1 回答 1

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;

否则,您将无法使用公共属性。

于 2013-10-05T02:10:59.430 回答