__get() と __set()とArrayAccessを見てください。
前者を使用すると、 のように非公開メンバーに$obj->foo
アクセスできるようになり、後者を使用すると、 のようにアクセスできます$obj['foo']
。
内部で好きなように配線できます。
個人的には、これらの魔法のようにアクセス可能なプロパティを、クラスの 1 つの配列メンバーに保持することをお勧めします。そうすれば、スパゲッティ コードになってしまうことはありません。
概念実証:
1 <?php
2 class Magic implements ArrayAccess {
3
4 protected $items = array();
5
6 public function offsetExists($key) {
7 return isset($this->items[$key]);
8 }
9 public function offsetGet($key) {
10 return $this->items[$key];
11 }
12 public function offsetSet($key, $value) {
13 $this->items[$key] = $value;
14 }
15 public function offsetUnset($key) {
16 unset($this->items[$key]);
17 }
18
19 //do not modify below, this makes sure we have a consistent
20 //implementation only by using ArrayAccess-specific methods
21 public function __get($key) {
22 return $this->offsetGet($key);
23 }
24 public function __set($key, $value) {
25 $this->offsetSet($key, $value);
26 }
27 public function __isset($key) {
28 return $this->offsetExists($key);
29 }
30 public function __unset($key) {
31 $this->offsetUnset($key);
32 }
33 }
34
35 //demonstrate the rountrip of magic
36 $foo = new Magic;
37 $foo['bar'] = 42;
38 echo $foo->bar, PHP_EOL;//output 42
39 $foo->bar++;
40 echo $foo['bar'];//output 43
41
一貫性ミロード、まさにあなたが求めたとおりです。