About the use of PHP constructors secret _php Tutorial

Source: Internet
Author: User
PHP after a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. Most classes have a special method called constructors. When an object is created, it automatically calls the PHP constructor, which is called when the object is instantiated using the New keyword.

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. 0class person
  3. 0{
  4. The following is the person's member property
  5. var $name; The name of the man
  6. var $sex; Man's Sex
  7. var $age; The age of the person
  8. Define a constructor method parameter for name $name, gender $sex, and age $age
  9. function __construct ($name, $sex, $age)
  10. {
  11. //$name to member properties by means of construction method $this- > name to assign the value
  12. $this- > name = $name;
  13. //$sex to member properties by means of construction method $this- > sex gives the first value
  14. $this- > Sex = $sex;
  15. //$age to member properties by means of construction method $this- > The age of the initial value
  16. $this- > Age = $age;
  17. }
  18. The way this guy talks.
  19. function Say ()
  20. {
  21. echo "My name is called:". $this- > name. "Gender:". $this- > sex. "My age is:". $this- > Age . " <br> ";
  22. }
  23. }
  24. Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments for name, gender, and age, respectively
  25. $ P1 = New Person ("Zhang San", "male", 20);
  26. $ P2 = New Person ("John Doe", "female", 30);
  27. $ P3 = New Person ("Harry", "male", 40);
  28. The following accesses the speech method in the $p1 object
  29. $p 1- > say ();
  30. The following accesses the speech method in the $p2 object
  31. $p 2- > say ();
  32. The following accesses the speech method in the $p3 object
  33. $p 3- > say ();



The output is:
My name is called: 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


http://www.bkjia.com/PHPjc/446532.html www.bkjia.com true http://www.bkjia.com/PHPjc/446532.html techarticle PHP After a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. Most classes have a special method called constructors. ...

  • 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.