Yii CList 错误 - 没有名为 "setReadOnly" 的方法或闭包



我有一个使用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_dump
object(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

clist方法setReadOnly()受保护,因此不能从您使用的范围中调用,仅来自自身内部或继承类。请参阅http://php.net/manual/en/language.oop5.5.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时,您可以在末尾设置读取的true,但是MergeWith()仅合并_D数据数组,而不是其他类变量,因此它仍然是错误的。

$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)

相关内容

  • 没有找到相关文章

最新更新