带有__set()和__get()的多维数组

时间:2012-05-08 23:36:17

标签: php methods multidimensional-array magic-methods

  

可能重复:
  Using __set with arrays solved, but why?

是否可以使用PHP魔术__set()__get()方法设置多维数组?即$Foo->arrayKey['anotherKey']='newValue';会设置$foo->_data['arrayKey']['anotherKey']=='newValue';

//example setting values to $_data[$k]
class Foo
{
   private $_data=array();

   public function __set($k, $v)
   {
       $this->_data[$k]=$v;
   }

   public function __set($k)
   {
       return $this->_data[$k];
   }
}

0 个答案:

没有答案