03-php Object-Oriented Programming-Constructor

Source: Internet
Author: User
<? 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.

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.