类中嵌套类,再嵌套数组和类,整糊涂了,求解
<br />class Editor<br />{<br /> var $Error = '' ;<br /> var $Pages;<br /> function vol()<br /> {<br /> $Pages = array();<br /> $this->Pages[] = new page;<br /> }<br />}<br /><br />class Page<br />{<br /> var $Id = '';<br /> var $BackImage;<br /> function vol()<br /> {<br /> $BackImage = array();<br /> $this->$BackImage[] = new Decorator;<br /> }<br />}<br /><br />class Decorator<br />{<br /> var $Ext = '';<br />}<br /><br />_________________________________________________________<br /><br />//我在使用这个类的时候<br />$Editor = new Editor;<br />$Editor->vol();<br />$Editor->Pages[0]->vol(); //对象定义完成?我只初始化了Pages[0]下的class Decorator对象,<br />为什么下面我可以直接使用page[2]和所有page数组其他元素中的class Decorator ,<br />而不需要我$Editor->Pages[2]->vol()呢?<br /><br /><br />$Editor->Pages[0]->BackImage[0]->Ext = 'str' ;<br />$Editor->Pages[2]->BackImage[2]->Ext = 'str2' ;<br />$Editor->Pages[5]->BackImage[4]->Ext = 'str6' ; //这样使用没有报错<br /><br />
class Editor<br />{<br /> var $Error = '' ;<br /> var $Pages;<br /> function vol()<br /> {<br /> $Pages = array();<br /> $this->Pages[] = new page;<br /> }<br />}<br /> <br />class Page<br />{<br /> var $Id = '';<br /> var $BackImage;<br /> function vol()<br /> {<br /> $BackImage = array();<br />// $this->$BackImage[] = new Decorator; //Fatal error: Cannot use [] for reading<br /> $this->BackImage[] = new Decorator; //只能这样写,因为 $BackImage 是数组<br /> }<br />}<br /> <br />class Decorator<br />{<br /> var $Ext = '';<br />}<br /> <br />$Editor = new Editor;<br />$Editor->vol();<br />$Editor->Pages[0]->vol(); <br /> <br />$Editor->Pages[0]->BackImage[0]->Ext = 'str' ;<br />$Editor->Pages[2]->BackImage[2]->Ext = 'str2'; //Warning: Creating default object from empty value
<br />strobject(Editor)#1 (2) { ["Error"]=> string(0) "" ["Pages"]=> array(2) { [0]=> object(Page)#2 (2) { ["Id"]=> string(0) "" ["BackImage"]=> array(1) { [0]=> object(Decorator)#3 (1) { ["Ext"]=> string(0) "" } } } <br /><br />[10]=> object(stdClass)#4 (1) { ["BackImage"]=> array(1) { [10]=> object(stdClass)#5 (1) { ["Name"]=> string(3) "str" } } } } }<br />