我有这个课让我可以使用data
方法更改私有setData
属性:
abstract class FooBase{
public function __set($name, $value){
$setter = 'set'.ucfirst($name);
if(method_exists($this, $setter)) return $this->$setter($value);
throw new Exception("Property {$setter} is not defined.");
}
}
class Foo extends FooBase{
static $instance;
private $data;
public static function app(){
if(!(self::$instance instanceof self)){
self::$instance = new self();
self::app()->data = array('a' => 'somedata', 'b' => 'moredata');
}
return self::$instance;
}
public function setData($newdata){
$this->data = $newdata;
}
}
所以要改变它,我称之为:
Foo::app()->data = array('a' => 'newdata', 'b' => 'morenewdata');
我想知道是否有可能以某种方式仅更改$data
中的一个数组值,例如:
Foo::app()->data['a'] = 'newdata'; // <-- this doesn't work, but it's what I would like to do...
答案 0 :(得分:2)
如果数据是公开的而不是私有的,您可以这样做。私有意味着只有对象可以在内部获得该值。公众会允许你这样做。或者创建一个方法来做你想做的事情,因为这可以在内部访问数组,使数据保密。