Php member variables can be initialized at the same time as they are declared, but only scalar variables can be initialized. 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 ();
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