Analysis of usage of PHP constructor function

Source: Internet
Author: User

The declaration of the PHP constructor is the same as the declaration of the other operation, except that its name must be __construct (). This is a change in PHP5, in the previous version, the name of the constructor must be the same as the class name, which can still be used in PHP5, but is now used by very few people, the advantage is that the constructor is independent of the class name, when the class name changes, you do not need to change the corresponding constructor name. To be backwards compatible, if a class does not have a method named __construct (), PHP will search for a constructor in PHP4 that has the same name as the class name. Format: function __construct ([parameter]) {...} Only one construction method can be declared in a class, but a constructor is called every time the object is created, and the method cannot be invoked actively, so it is often used to perform some useful initialization tasks. For example, an attribute is assigned an initial value when creating an object.  1. Create a human  2.  3. 0class person   4. 0{  5.//The following is a member of the person attribute   6. var $name;      //person's name   7. var $sex;        //human gender   8. var $age;        //person's age   9. Defines a constructor method parameter for name $name, gender $sex, and age $age   10. function __construct ($name, $sex, $age)   11. {  12.//The $name that are passed through the constructor method to the member property $this->name the value   13. $this->name= $name;   14.// The $sex passed in by the constructor method assigns the value   15 to the member property $this->sex. $this->sex= $sex;   16. The $age passed in by the constructor method assigns the value   17 to the member property $this->age. $this->aGe= $age;   18. }   19. The man's way of speaking   20. function say ()   21. { 22. echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> ";   23. }   24. }   25. Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments for name, gender, and age, respectively  26. $p 1=new person ("Zhang San", "male", 20);   27. $p 2=new person ("John Doe", "female", 30);   28. $p 3=new person ("Harry", "male", 40);   29. The following accesses the speech method   30 in the $p1 object. $p 1->say ();   31. The following accesses the speech method   32 in the $p2 object. $p 2->say ();   33. The following accesses the speech method   34 in the $p3 object. $p 3->say ();    output results for:  my name: Zhang San Sex: Male my age is:20  my name is called: John Doe Sex: Female my age is: 30 My name is called: Harry Sex: Male My age is: 40

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.