2013-10-05 58 views
1

我想的對象從類用戶進行轉換,但是當我完成創建我的用戶,並嘗試使用json_encode把它轉換,它返回我一個空的JSONObject - >{} 這裏是我的類用戶:不能簡單對象轉換爲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

所有的類屬性都是私有的,json_encode無法看到它們對它們進行編碼 –

+0

[PHP json \ _encode類私有成員]的可能重複(http://stackoverflow.com/questions/7005860/php-json-encode-class -private-members) –

+0

嗨,謝謝你,工作!你可以發佈你的答案,而不是我可以驗證它嗎? – R00t

回答

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; 

否則,你堅持使用公共屬性。

+0

這是一個很好的解決方案,絕對是我會用的!謝謝,我會在4分鐘內接受你的回答! – R00t

相關問題