简体   繁体   中英

How can i access a private property in Object

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
                )
        )
)

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM