Php static variable initialization _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php static variable initialization. For example: classA {public $ f1xxxx; staticpublic $ f2100;} to assign a variable to an object, you can only initialize the variable in the constructor. for example: classA {private $ chi, for example:
Class {
Public $ f1 = 'xxxx ';
Static public $ f2= 100;
}


If you want to assign a variable to an object, you can only initialize the variable in the constructor. for example:
Class {
Private $ child;
Public function _ construct (){
$ This-> child = new B ();
}
}


However, php does not have anything similar to the static constructor/static block in java, so there is no proper time to initialize it.


There are still solutions for common members, such:
Class {
Static public $ child;
}
A: $ child = new B ();


There seems to be no clean method for private members. you can only do this:
Class {
Static private $ child;
Static public initialize (){
Self: $ child = new B ();
}
}
A: initialize ();

Keep class A {public $ f1 = 'xxxx'; static public $ f2 = 100;} to assign A variable to an object, you can only initialize the variable in the constructor. for example: class A {private $ chi...

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.