首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii CList错误-没有名为"setReadOnly“的方法或闭包

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

Stack Overflow用户
提问于 2014-02-04 15:46:37
回答 1查看 315关注 0票数 0

我有一个使用CList的基本函数--由于某种原因,我得到了以下错误:

代码语言:javascript
复制
CList and its behaviors do not have a method or closure named "setReadOnly".

我的php代码

代码语言:javascript
复制
$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 .我直接从最近的一本书中复制了这段代码.所以我有点困惑

// Update:在var_dump ()之前和之后添加mergeWith()

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 17:11:25

CList方法setReadOnly()是受保护的,因此不能从您使用的作用域调用,只能从它本身或继承类调用。见http://php.net/manual/en/language.oop5.visibility.php#example-188

但是,CList类允许在其构造函数中将列表设置为只读。

代码语言:javascript
复制
public function __construct($data=null,$readOnly=false)
{
    if($data!==null)
    $this->copyFrom($data);
    $this->setReadOnly($readOnly);
}

所以..。

代码语言:javascript
复制
$list = new CList(array('python', 'ruby'), true); // Passing true into the constructor
$anotherList = new CList(array('php'));
$list->mergeWith($anotherList);

导致错误

代码语言:javascript
复制
CException The list is read only. 

我不确定这是否是您正在寻找的结果,但是如果您想要一个只读CList,这是获得它的一种方法。

您可能认为在合并后续的CLists时,可以在末尾设置readonly true,但是mergeWith()只合并_d数据数组,而不是其他类变量,因此它仍然是假的。

代码语言:javascript
复制
$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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21556912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档