Secrets about PHP constructor usage

Source: Internet
Author: User

PHP has been developing for a long time and many users are familiar with PHP. Here I will share my personal understanding and discuss it with you. Most classes have a special method called constructor. When an object is created, it automatically calls the PHP constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called.

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 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 are the member attributes of a person.
  6. Var $ name; // name of a person
  7. Var $ sex; // gender of a person
  8. Var $ age; // age of a person
  9. // 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->Initial Value assigned by name
  13. $ This->Name= $ Name;
  14. // $ Sex passed in through the constructor to the member attribute $ this->Sex assigned Initial Value
  15. $ This->Sex= $ Sex;
  16. // The $ age passed in through the constructor gives the Member attributes $ this->Age assigned Initial Value
  17. $ This->Age= $ Age;
  18. }
  19. // How this person speaks
  20. Function say ()
  21. {
  22. Echo "My name is:". $ this->Name. "Gender:". $ this->Sex. "My age is:". $ this->Age ."<Br>";
  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=NewPerson ("Zhang San", "male", 20 );
  27. $P2=NewPerson ("Li Si", "female", 30 );
  28. $P3=NewPerson ("Wang Wu", "male", 40 );
  29. // The following method of accessing the $ p1 object
  30. $ P1->Say ();
  31. // The following method of accessing the $ p2 object
  32. $ P2->Say ();
  33. // The following method is used to access the $ p3 object.
  34. $ P3->Say ();



Output result:
My name is John. Gender: male. My age is: 20.
My name is Li Si Gender: Female my age is: 30
My name is: Wang Wu Gender: 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.