我有一个使用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 .我直接从最近的一本书中复制了这段代码.所以我有点困惑
// Update:在var_dump ()之前和之后添加mergeWith()
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发布于 2014-02-04 17:11:25
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数据数组,而不是其他类变量,因此它仍然是假的。
$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)https://stackoverflow.com/questions/21556912
复制相似问题