2013-10-05 3 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 class private members] (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

관련 문제