Home > Net >  How can i access a private property in Object
How can i access a private property in Object

Time:03-17

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

CodePudding user response:

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

?>
  •  Tags:  
  • php
  • Related