1

I am using couchbase PHP Sdk to query some data and they made some changes from version 2.x to 3.x. Before i could get some metrics easily now I get the error> It complains that I cant access private value.

Uncaught Error: Cannot access private property Couchbase\QueryResultImpl::$meta

Couchbase\QueryResultImpl Object
(
    [status:Couchbase\QueryResultImpl:private] => 0
    [meta:Couchbase\QueryResultImpl:private] => Couchbase\QueryMetaDataImpl Object
        (
            [status:Couchbase\QueryMetaDataImpl:private] => success
            [request_id:Couchbase\QueryMetaDataImpl:private] => b1c2bfc4-31b1-4c17-9706-2d0b1e574505
            [client_context_id:Couchbase\QueryMetaDataImpl:private] => d41ef78df23fffe4
            [signature:Couchbase\QueryMetaDataImpl:private] => Array
                (
                    [ModificationTimestamp] => json
                )

            [errors:Couchbase\QueryMetaDataImpl:private] => 
            [warnings:Couchbase\QueryMetaDataImpl:private] => 
            [metrics:Couchbase\QueryMetaDataImpl:private] => Array
                (
                    [elapsedTime] => 13.425556ms
                    [executionTime] => 13.228202ms
                    [resultCount] => 1
                    [resultSize] => 47
                    [serviceLoad] => 0
                )

        )

    [rows:Couchbase\QueryResultImpl:private] => Array
        (
            [0] => Array
                (
                    [ModificationTimestamp] => 2022-03-14 13:06:42
                )
        )
)
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
MisterniceGuy
  • 1,646
  • 2
  • 18
  • 41
  • 2
    Hello @MisterNiceGuy, You can create a **getter** and **setter** method for that private property – AwatITWork Mar 16 '22 at 21:03
  • How and where would i do that ? – MisterniceGuy Mar 16 '22 at 21:29
  • 1
    It would be possible with reflection: https://www.php.net/manual/en/reflectionproperty.setaccessible.php – Michael Bolli Mar 16 '22 at 21:45
  • 3
    Actually, this behaviour is by design and you are not supposed to access private members... If the class is not under your control and the class does not already have getter/setter, then the developer probably hid the attribute for a good reason – Honk der Hase Mar 16 '22 at 23:33

1 Answers1

2

First of all, I am sorry I didn't use couchbase PHP, but here is how you can create a Getter and Setter method for a Private variable, hope you can implement it

<?php

class Student {
    private $sName;

    public function setName($name) {
        $this->sName = $name;
    }

    public function getName() {
        return $this->sName;
    }
}

$student = new Student;                     // create an object
//$student->sName = "error";                // this makes an error while it is Private: Fatal error: Uncaught Error: Cannot access private property
//echo $student->sName;                     // this makes an error while it is Private: Fatal error: Uncaught Error: Cannot access private property
$student->setName("MisterniceGuy"); // Set student name
echo $student->getName();                   // MisterniceGuy

?>
AwatITWork
  • 556
  • 1
  • 5
  • 13