PHP constructor usage analysis _ PHP Tutorial

Source: Internet
Author: User
Analysis of PHP constructor usage. The PHP constructor declaration is the same as that of other operations, but its name must be _ construct (). This is a change in PHP5. in earlier versions, the constructor name must be the same as the class name. The PHP constructor declaration is the same as that of other operations, the name must be _ construct (). This is a change in PHP5. in previous versions, the name of the constructor must be the same as the class name, which can still be used in PHP5, but it is rarely used now, in this way, the constructor can be made independent of the class name. when the class name changes, you do not need to change the corresponding constructor name. For backward compatibility, if a class does not have a method named _ construct (), PHP will search for a method written in php4 with the same name as the class name. Format: function _ construct ([parameter]) {… ... } Only one constructor can be declared in a class. Instead, the constructor is called only once every time an object is created, so it is usually used to execute some useful initialization tasks. For example, assign an initial value to a property when creating an object. 1. // create a human 2. 3. 0 class Person 4. 0 {5. // The following is the member attribute of a person. 6. var $ name; // name of a person 7. var $ sex; // gender of a person. var $ age; // The age of a person. // define a constructor parameter: name $ name, gender $ sex, and age $ age 10. function _ construct ($ name, $ sex, $ age) 11. {12. // assign the $ name passed in by the constructor to the member attribute $ this-> name with the initial value 13. $ this-> name = $ name; 14. // The $ sex passed in through the constructor gives the Member attribute $ this-> sex the initial value of 15. $ this-> sex = $ sex; 16. // assign the initial value 17 to the member attribute $ this-> age through the $ age passed in by the constructor. $ this-> age = $ age; 18 .} 19. // The way this person speaks 20. function say () 21. {22. echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
"; 23 .} 24 .} 25. // create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age 26. $ p1 = new Person ("zhang san", "male", 20); 27. $ p2 = new Person ("Li Si", "female", 30); 28. $ p3 = new Person ("Wang Wu", "male", 40); 29. // Access Method 30 in the $ p1 object below. $ p1-> say (); 31. // access the method 32 in the $ p2 object below. $ p2-> say (); 33. // method 34 for accessing the $ p3 object below. $ p3-> say (); the output result is: My name is Zhang San Gender: Male my age is: 20 my name is Li Si gender: female: 30 my name is: Wang Wu Gender: Male my age is: 40

Merge (). This is a change in PHP5. in previous versions, the constructor name must be the same as the class name...

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.