PHP-将对象数组保存到文件并重用它-调用未定义的方法时出错
我第一次在这里发帖,所以请耐心等待 我目前正在做以下工作:我需要用php编写一个留言簿。我有一份新的参赛表格。条目由类表示:PHP-将对象数组保存到文件并重用它-调用未定义的方法时出错,php,arrays,json,object,store,Php,Arrays,Json,Object,Store,我第一次在这里发帖,所以请耐心等待 我目前正在做以下工作:我需要用php编写一个留言簿。我有一份新的参赛表格。条目由类表示: class entry{ private $name; private $email; private $text; ... construct-method and a output() method for html code ... function getName() {return $this-&
class entry{
private $name;
private $email;
private $text;
...
construct-method and a output() method for html code
...
function getName()
{return $this->name;}}
提交表单时,我调用“addToGuestbook.php”,在这里我读取$_POST[…]并创建一个新条目:
$ent = new entry($name, $email, $text);
由于我想存储条目以供进一步使用,我将其放入一个数组中,并在json的帮助下保存它:
$entries = json_decode(file_get_contents('entries.json'), false);
$entries[] = $ent;
file_put_contents("entries.json",json_encode($entries));
在此之后,我想操作其中一个html页面,因此我包含一个函数文件并调用函数
include "functions.php";
setUpSingleEntries();
。。。在其中,我迭代存储的数组
$entries = json_decode(file_get_contents('entries.json'), false);
foreach($entries as $entry)
{
if($entry->getName() == $name)
{
fwrite($file, $entry->output());
}
}
当我运行整个程序时,我在第32行的C:\XAMPP\htdocs\ex9\functions.php中得到了对未定义方法stdClass::getName()的调用,错误是,与第32行相同
如果($entry->getName()=$name)
我已经做得很好了,因为几天以来我一直在尝试编写留言簿,但我无法解决这些错误,因为我还没有找到解决方案。json\u decode没有返回类型为“entry”的对象,因此出现了错误(返回的对象没有定义getName方法,也没有输出方法)
从
您的代码需要更正(或者按照其他答案中的建议,使条目实现JsonSerializable):
希望能有帮助
问候。如果要通过JSON序列化对象,必须首先实现接口。假设您使用的是PHP5.4+(您应该这样做):
这将把对象序列化为关联数组。但是,您不能直接将(json_decode
)取消序列化到该对象中,因此需要添加某种类型的取消序列化方法,该方法将从\stdClass
为您构建对象。例如:
class Entry implements \JsonSerializable
{
private $name;
private $email;
private $text;
// assuming you have a constructor like this
public function __construct($name, $email, $text)
{
$this->name = $name;
$this->email = $email;
$this->text = $text;
}
public function jsonSerialize()
{
return array(
'name' => $this->name,
'email' => $this->email,
'text' => $this->text
);
}
public static function fromStdClass(\stdClass $entry)
{
// here you can do some validation if $entry actually has these properties
return new self($entry->name, $entry->email, $entry->text);
}
}
现在您可以使用如下代码:
$entries = json_decode(file_get_contents('entries.json'), false);
foreach ($entries as $entry) {
$entryObj = Entry::fromStdClass($entry);
if ($entryObj->getName() == $name) {
fwrite($file, $entryObj->output());
}
}
$entries = json_decode(file_get_contents('entries.json'), false);
foreach($entries as $obj)
{
if($obj->{'name'} == $name)
{
fwrite($file, /*Something else*/);// $entry->output() won't work, figure something else out
}
}
class Entry implements \JsonSerializable
{
private $name;
private $email;
private $text;
public function jsonSerialize()
{
return array(
'name' => $this->name,
'email' => $this->email,
'text' => $this->text
);
}
}
class Entry implements \JsonSerializable
{
private $name;
private $email;
private $text;
// assuming you have a constructor like this
public function __construct($name, $email, $text)
{
$this->name = $name;
$this->email = $email;
$this->text = $text;
}
public function jsonSerialize()
{
return array(
'name' => $this->name,
'email' => $this->email,
'text' => $this->text
);
}
public static function fromStdClass(\stdClass $entry)
{
// here you can do some validation if $entry actually has these properties
return new self($entry->name, $entry->email, $entry->text);
}
}
$entries = json_decode(file_get_contents('entries.json'), false);
foreach ($entries as $entry) {
$entryObj = Entry::fromStdClass($entry);
if ($entryObj->getName() == $name) {
fwrite($file, $entryObj->output());
}
}