0

I've got a problem. I got 2 PHP files that each echo a json_encode() of an array with objects (one an array of users and one an array of rewards). However rewards works perfectly fine while users returns [{},{},{}]

here's my users.php

<?php

header('Content-Type: application/json');

require('../logic/User.php');
require('../database/Database.php');

$database = new Database();

if(isset($_GET['id'])) {
    $user = $database->getUserById($_GET['id']);
    if($user != false){
        echo json_encode($user);
    }else{
        echo 'User not found';
    }
}else {
    $users = $database->getUsers();
    echo json_encode($users);
}
?>

And here's my User class (without magic methods):

class User implements JsonSerializable
{
    private $id;
    private $name;
    private $birthday;
    private $gender;
    private $address;
    private $postalcode;
    private $phonenr;
    private $email;
    private $facebookid;

    private $accessible = array('id', 'name', 'birthday', 'gender', 'address', 'postalcode', 'phonenr', 'email', 'facebookid');
    private $editable = array('name', 'birthday', 'gender', 'address', 'postalcode', 'phonenr', 'email', 'facebookid');
    private $required = array('id', 'name');

//getter, setter, constructor

    public function jsonSerialize(){
        return ['id' => $this->id,
            'name'=>$this->name,
            'birthday'=>$this->birthday,
            'gender'=>$this->gender,
            'address'=>$this->address,
            'postalcode'=>$this->postalcode,
            'phonenr'=>$this->phonenr,
            'email'=>$this->email,
            'facebookid'=>$this->facebookid
        ];
    }
}

$users is filled, so the problems must be the json_encode()

3
  • what should returns? Commented Jan 14, 2016 at 11:21
  • What are the types of private properties? If some of your properties are objects they should also implement JsonSerializable. Commented Jan 14, 2016 at 11:29
  • the properties are all strings, ints or dates (which I believe is just a string) Commented Jan 14, 2016 at 11:45

1 Answer 1

1

Its because the user properties are private. If you make them public they will show.

However, decide if it you want to change them to public because they are probably private for a reason.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.