Accessing Private Members in PHP with __get(), __set() and ArrayAccess

This article explains why PHP throws "attempt to access private member" errors, describes the visibility keywords public, protected, private, and demonstrates how to use the magic methods __get() and __set() as well as the ArrayAccess interface to read and write private properties from outside the class.

php Courses
php Courses
php Courses
Accessing Private Members in PHP with __get(), __set() and ArrayAccess

In PHP development, attempting to access a class member declared as private triggers an "attempt to access private member" error. The article first reviews PHP's visibility modifiers: public (accessible everywhere), protected (accessible within the class and its subclasses), and private (accessible only inside the defining class).

When external code needs to read or modify a private property, PHP's magic methods __get() and __set() can be employed. The article provides a complete example showing a class MyClass that implements these methods to expose a private member $privateMember through property-like syntax.

class MyClass {
   private $privateMember;

   public function __get($name) {
      if ($name === 'privateMember') {
         return $this->privateMember;
      }
   }

   public function __set($name, $value) {
      if ($name === 'privateMember') {
         $this->privateMember = $value;
      }
   }
}

$obj = new MyClass();
$obj->privateMember = 'Hello, world!'; // set private member
echo $obj->privateMember; // get and output value

The example demonstrates that, after defining __get() and __set(), the private member can be accessed using the usual $obj->privateMember syntax.

The article also addresses accessing private members via array syntax, which also raises errors. By implementing the ArrayAccess interface, a class can allow array-style access to private properties. A second code example shows how to implement the required methods ( offsetExists, offsetGet, offsetSet, offsetUnset) to manipulate $privateMember using $obj['privateMember'].

class MyClass implements ArrayAccess {
   private $privateMember;

   public function offsetExists($offset) {
      return $offset === 'privateMember';
   }

   public function offsetGet($offset) {
      return $this->$offset;
   }

   public function offsetSet($offset, $value) {
      $this->$offset = $value;
   }

   public function offsetUnset($offset) {
      unset($this->$offset);
   }
}

$obj = new MyClass();
$obj['privateMember'] = 'Hello, world!'; // set private member
echo $obj['privateMember']; // get and output value

In summary, when encountering the "attempt to access private member" error in PHP, developers can resolve it by using the magic methods __get() and __set() to expose private properties, or by implementing the ArrayAccess interface to enable array-style access, providing flexible ways to manage encapsulation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPObject-Orientedmagic methodsArrayAccessprivate members
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.