我有一个使用CList的基本功能 - 由于某种原因,我收到以下错误:
CList and its behaviors do not have a method or closure named "setReadOnly".
我的PHP代码
$list = new CList(array('python', 'ruby'));
$anotherList = new Clist(array('php'));
var_dump($list);
$list->mergeWith($anotherList);
var_dump($list);
$list->setReadOnly(true); // CList and its behaviors do not have a method or closure named "setReadOnly".
任何人都可以解释我收到此错误的原因吗?
P.S我直接从最近的Yii书中复制了这段代码......所以我感到很困惑
//更新:在mergeWith()
之前和之后添加了var_dumpobject(CList)[20]
private '_d' =>
array (size=2)
0 => string 'python' (length=6)
1 => string 'ruby' (length=4)
private '_c' => int 2
private '_r' => boolean false
private '_e' (CComponent) => null
private '_m' (CComponent) => null
object(CList)[20]
private '_d' =>
array (size=3)
0 => string 'python' (length=6)
1 => string 'ruby' (length=4)
2 => string 'php' (length=3)
private '_c' => int 3
private '_r' => boolean false
private '_e' (CComponent) => null
private '_m' (CComponent) => null
答案 0 :(得分:1)
CList方法setReadOnly()受到保护,因此无法从您正在使用的作用域中调用,只能从其自身内部或继承类中调用。请参阅http://php.net/manual/en/language.oop5.visibility.php#example-188。
但是,CList类允许列表在其构造函数
中设置为只读public function __construct($data=null,$readOnly=false)
{
if($data!==null)
$this->copyFrom($data);
$this->setReadOnly($readOnly);
}
因此...
$list = new CList(array('python', 'ruby'), true); // Passing true into the constructor
$anotherList = new CList(array('php'));
$list->mergeWith($anotherList);
导致错误
CException The list is read only.
我不确定这是否是你正在寻找的结果,但如果你想要一个只读的CList,那就是获得它的一种方式。
您可能会认为在合并后续CLists时,您可以在结尾处设置readonly true,但mergeWith()仅合并_d数据数组,而不是其他类变量,因此它仍为false。
$list = new CList(array('python', 'ruby'));
$anotherList = new CList(array('php'));
$yetAnotherList = new CList(array('javacript'), true);
$list->mergeWith($anotherList);
$list->mergeWith($yetAnotherList);
var_dump($list); // ["_r":"CList":private]=>bool(false)