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 が必要な場合は、それを取得する方法の 1 つです。

後続の 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 に答える