【发布时间】:2020-02-06 00:10:58
【问题描述】:
我正在尝试构建一个可重用的 db 类,它允许我针对我的 couchbase db 访问基本的 crud 功能。当我尝试执行此函数时,出现以下错误:
PHP Notice: Undefined variable: bucket in
/var/www/html/PHRETS/couchBase.php on line 26 PHP Fatal error:
Uncaught Error: Call to a member function upsert() on null in
/var/www/html/PHRETS/couchBase.php:26 Stack trace:
#0 /var/www/html/PHRETS/retsphp.php(72): couchDb::upsert('OpenHouse::b769...', Object(OpenHouse))
#1 {main} thrown in /var/www/html/PHRETS/couchBase.php on line 26
所以问题是如何从类中的函数访问 $bucket 对象?
这是我在 couchBase.php 中的代码
<?php
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
$bucketName = "default";
// Establish username and password for bucket-access
$authenticator = new \Couchbase\PasswordAuthenticator();
$authenticator->username('Administrator')->password('Password');
// Connect to Couchbase Server - using address of a KV (data) node
$cluster = new CouchbaseCluster("couchbase://127.0.0.1");
// Authenticate, then open bucket
$cluster->authenticate($authenticator);
$bucket = $cluster->openBucket($bucketName);
class couchDb {
public function upsert($DocId, $doc)
{
$result = $bucket->upsert($DocId, $doc);
return ($result->cas);
}
}
【问题讨论】:
-
对于初学者来说,将您的存储桶声明移到函数上方的类中
-
请注意,CouchDB 和 Couchbase 不是一回事,我建议重命名你的类以避免混淆