<?php
class test{
private $k=array();
public function __set($a,$p){
$this->k[$a]=$p;
}
public function __get($a){
return $this->k[$a];
}
}
$t=new test();
$t->M="10"; // this works.
$t->P=array(); // this works.
$t->P=array("def"=>"456"); // this works.
$t->P["abc"]="123"; // this doesn't work
var_dump($t->P);
?>
是否有可能这样做$ t-> P [“ abc”] =“ 123”还是有其他类似的方法?
预先感谢
答案 0 :(得分:0)
以下是带有解释的解决方案:
theCB
输出:
<?php
class test{
private $k=array();
public function __set($a,$p){
var_dump( "SET: $a=$p" );
$this->k[$a]=$p;
}
public function &__get($a) { // add "&" here to return reference instead of value!
var_dump( "GET: " . $a );
return $this->k[$a];
}
}
$t=new test();
$t->P['abc3'] = "1233"; // triggers GET // gets set by reference
var_dump( $t->P['abc3'] ); // triggers GET for $t->P then returns value of(<ref>)['abc3']
var_dump($t->P); // triggers GET