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.
- Create a human
- 0class person
- 0{
- The following is the person's member property
- var $name; The name of the man
- var $sex; Man's Sex
- var $age; The age of the person
- Define a constructor method parameter for name $name, gender $sex, and age $age
- function __construct ($name, $sex, $age)
- {
- //$name to member properties by means of construction method $this- > name to assign the value
- $this- > name = $name;
- //$sex to member properties by means of construction method $this- > sex gives the first value
- $this- > Sex = $sex;
- //$age to member properties by means of construction method $this- > The age of the initial value
- $this- > Age = $age;
- }
- The way this guy talks.
- function Say ()
- {
- echo "My name is called:". $this- > name. "Gender:". $this- > sex. "My age is:". $this- > Age . " <br> ";
- }
- }
- Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments for name, gender, and age, respectively
- $ P1 = New Person ("Zhang San", "male", 20);
- $ P2 = New Person ("John Doe", "female", 30);
- $ P3 = New Person ("Harry", "male", 40);
- The following accesses the speech method in the $p1 object
- $p 1- > say ();
- The following accesses the speech method in the $p2 object
- $p 2- > say ();
- The following accesses the speech method in the $p3 object
- $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. ...