<? PHP // ① the constructor does not return a value // ② it is called by the system itself. Class person {public $ name; Public $ age; // constructor in PhP4 # public function person () {# echo "OK "; #}// the constructor in PhP5 includes the above and below. If both methods are together, the second # public function _ construct () is preferred () {# echo "I'm a constructor"; #}// this constructor public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age ;}}$ person1 = new person ("Sun Wukong", 2000); echo $ person1-> name;?>
Note:
1. the PhP4 constructor name can be the same as the class name.
2. The role of constructor is object initialization.
3. When a new object is created, the system automatically calls the constructor.
4. There is only one constructor in a class.
5. If no custom constructor is available, the system will call the default constructor.
6. If the system contains the default constructor and custom constructor, the system will call the custom constructor.
7. The default access modifier of the constructor is public.