0

我有一个使用 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". 

谁能解释我为什么会收到这个错误?

PS我直接从最近的一本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
4

1 回答 1

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,这是获得它的一种方法。

您可能认为在合并后续 CList 时可以在最后设置 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)
于 2014-02-04T17:11:25.793 回答