gpt4 book ai didi

mongodb - 无法在 mongodb 中保存 PHP 对象

转载 作者:可可西里 更新时间:2023-11-01 09:19:58 33 4
gpt4 key购买 nike

保存对象时出现“不允许使用零长度键”的 fatal error 。

是因为 __construct() 的存在吗?

我相信 PHP 对象在 save() 方法中是允许的。

class Address{
private $name;
private $company;
private $zip;

public function __construct($name,$company,$zip){
$this->name = $name;
$this->company = $company;
$this->zip = $zip;
}
}

$newAddress = new Address("james","google",678);

print_r($newAddress);
// Address Object ( [name:Address:private] => james [company:Address:private] =>
// google [zip:Address:private] => 678 )

$addresses->save($newAddress);

Fatal error: Uncaught exception 'MongoException' with message 'zero-length keys are not allowed, did you use $ with double quotes?' in /var/www/html/index.php:105 Stack trace: #0 /var/www/html/index.php(105): MongoCollection->save(Object(Address)) #1 {main} thrown in /var/www/html/index.php on line 105

最佳答案

嗯,您希望 mongo 如何读取您的私有(private)类变量。

关于mongodb - 无法在 mongodb 中保存 PHP 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9809103/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com