0

I am wondering is it possible for me to access the _staff_id variable without me having to change the declaration to public (I have access to change this but its not my code and im assuming it was made private for a reason, however i am still tasked with getting this information)

MyObject Object
(

    [_staff_id:private] => 43

)
Styphon
  • 10,304
  • 9
  • 52
  • 86
Adrian
  • 1,976
  • 5
  • 41
  • 105
  • You can't access it from outside of the class, unless you class has a public getter method.... that's the whole point of private – Mark Baker Feb 26 '15 at 13:31
  • possible duplicate of [accessing private variable from member function in PHP](http://stackoverflow.com/questions/1762135/accessing-private-variable-from-member-function-in-php) – Jørgen R Feb 26 '15 at 13:33

1 Answers1

2

Using a public get function. E.g.:

class MyObject {
    private _staff_id = 43

    public function get($field) {
        return $this->$field;
    }
}
$myObject = new MyObject;
$staff_id = $myObject->get('_staff_id');

This allows you to access the variable without the ability to overwrite its value.

Styphon
  • 10,304
  • 9
  • 52
  • 86