0

I want to get value from object in array. Object property has been set to private. So, I could not access the value.

I try to convert private to public using Php ReflectionClass.

VarDump of Object In Array($obj_array)

array(1) 
{
  [23]=>
  object(PhpOffice\PhpSpreadsheet\Worksheet\RowDimension)#6167 (7) 
   {
    ["rowIndex":"PhpOffice\PhpSpreadsheet\Worksheet\RowDimension":private]=>
    int(23)
    ["height":"PhpOffice\PhpSpreadsheet\Worksheet\RowDimension":private]=>
    string(3) "7.5"
    ["zeroHeight":"PhpOffice\PhpSpreadsheet\Worksheet\RowDimension":private]=>
    bool(false)
    ["visible":"PhpOffice\PhpSpreadsheet\Worksheet\Dimension":private]=>
    bool(true)
    ["outlineLevel":"PhpOffice\PhpSpreadsheet\Worksheet\Dimension":private]=>
    int(0)
    ["collapsed":"PhpOffice\PhpSpreadsheet\Worksheet\Dimension":private]=>
    bool(false)
    ["xfIndex":"PhpOffice\PhpSpreadsheet\Worksheet\Dimension":private]=>
    NULL
    }
}

Code to convert private object to public

       foreach($obj_array as $key=>$value)
       {
         $r = new ReflectionObject($value);
         $p = $r->getProperty('height');
         $p->setAccessible(true); 
         echo $obj->height.'<br/>';
       }

I expect to get height value,7.5 from the object. It end up with this error.

Uncaught Error: Cannot access private property PhpOffice\PhpSpreadsheet\Worksheet\RowDimension::$height

Thanks in advance.

Premlatha
  • 1,676
  • 2
  • 20
  • 40

1 Answers1

2

Just use the getRowHeight function it returns the private height property internally. (as seen in the source of PhpSpreadSheet)

/**
 * Get Row Height.
 *
 * @return float
 */
public function getRowHeight()
{
    return $this->height;
}
Liam Martens
  • 731
  • 7
  • 22
  • Omg, yea. thanks a lot. I put these inside the loop . It works nicely. **$rowDimension = $obj_array [$key]; $rowHeight = $rowDimension->getRowHeight();** – Premlatha Apr 12 '19 at 07:11