我正在尝试将 php 中的类中的一些属性编码为 JSON,但我返回的所有方法都是 {}
这是我的代码,我哪里出错了?
谢谢。
<?php
class Person
{
private $_photo;
private $_name;
private $_email;
public function __construct($photo, $name, $email)
{
$this->_photo = $photo;
$this->_name = $name;
$this->_email = $email;
}
public function getJsonData() {
$json = new stdClass;
foreach (get_object_vars($this) as $name => $value) {
$this->$name = $value;
}
return json_encode($json);
}
}
$person1 = new Person("mypicture.jpg", "john doe", "doeman@gmail.com");
print_r( $person1->getJsonData() );