类中嵌套类,再嵌套数组和类,整糊涂了,求解

WBOY
Release: 2016-06-23 14:25:07
Original
1163 people have browsed it

本帖最后由 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' ; //这样使用没有报错
Copy after login


回复讨论(解决方案)

这个应该是会有 警告的

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 
Copy after login

连语法检查都通不过!

我来试试

[ color=#993300]感觉找到原因了:[/color]
//我在使用这个类的时候
$Editor = new Editor;
$Editor->vol();
$Editor->Pages[0]->vol(); //对象定义完成?我只初始化了Pages[0]下的class Decorator对象,
//为什么下面我可以直接使用page[2]和所有page数组其他元素中的class Decorator ,
//而不需要我$Editor->Pages[2]->vol()呢?
//$Editor->name = "xlc";
echo "


";

$Editor->Pages[10]->BackImage[10]->Name = 'str' ;
echo $Editor->Pages[10]->BackImage[10]->Name;
var_dump($Editor);

输出内容为:

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" } } } } }
Copy after login

请查数据Pages[10]的键值,其实他创建的是一人stdClass类页并不是Page类的一个实例,这个应该是php5特性吧。
刚开始以为是魔术方法里面问题,但后来确定了一下,不是的。


没有报错应该是你的php设置了报告级别

    {
        $BackImage = array(1,2,3,4);
        $this->BackImage[] = new Decorator;
        //这里增加两句或者可帮助你理解
        print_r($BackImage);
        print_r($this->BackImage);
    }
你需要了解一下类的属性和类内出现的一般变量的异同

建议你应该去学习 类的继承,能理清并简化你上面的思路,减少对象链、方法链的使用

谢谢楼上各位,有你们真好!再次感谢!!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!