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