Nested classes in the class, then nested arrays and classes, all confused. Finally, this post was edited by jerryleeee from 2013-10-2503: 26: 59 class & nbsp; editor {var & nbsp; $ Error & nbsp; var & nbsp; $ Pages; function & nbsp; vol () {$ Pages & Nested classes in the class, and then nested arrays and classes, confused, solve
This post was last edited by jerryleeee at 03:26:59, January 25 ,.
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 = '';
}
_________________________________________________________
// When I use this class
$ Editor = new Editor;
$ Editor-> vol ();
$ Editor-> Pages [0]-> vol (); // is the object definition complete? I only initialized the class Decorator object under Pages [0,
Why can I directly use the class Decorator in page [2] and all other elements of the page array,
What if I don't need $ 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'; // no error is returned when this operation is performed.
Share:
------ Solution --------------------
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; // only this can be written, because $ BackImage is an array
}
}
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
Even the syntax check is not passed!
------ Solution --------------------
[
Color = #993300] I found the cause: [/color]
// When I use this class
$ Editor = new Editor;
$ Editor-> vol ();
$ Editor-> Pages [0]-> vol (); // is the object definition complete? I only initialized the class Decorator object under Pages [0,
// Why can I directly use the class Decorator in page [2] and all other elements of the page array,
// What if I don't need $ 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 );
Output Content:
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" } } } } }
Please check the key value of data Pages [10]. In fact, the one-person stdClass Page is not an instance of the Page class. This should be the php5 feature.
At first, I thought it was a problem in the magic method, but I decided later, no.
------ Solution --------------------
If no error is reported, your php has set the Report level.
{
$ BackImage = array (1, 2, 4 );
$ This-> BackImage [] = new Decorator;
// Add two sentences here to help you understand
Print_r ($ BackImage );
Print_r ($ this-> BackImage );
}
You need to know the similarities and differences between class attributes and general variables in the class.
We recommend that you learn the inheritance of classes, clarify and simplify your above ideas, and reduce the use of object chains and method chains.