Nested classes in the class, then nested arrays and classes, confused, solved

Source: Internet
Author: User
Nested classes in the class, and nested arrays and classes are confused. This post was finally 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 in 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.


Reply to discussion (solution)

This should have a warning.

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; // this can only be written like this, 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!

Let me try it.

[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.


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.

Thank you, everyone upstairs. it's nice to have you! Thank you again !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.