Usage analysis of PHP constructor_PHP tutorial

WBOY
Release: 2016-07-13 10:30:08
Original
752 people have browsed it

The declaration of the PHP constructor is the same as the declaration of other operations, except that 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. This can still be used in PHP5, but now few people use it. The advantage of this is that the constructor can be Independent of the class name, there is no need to change the corresponding constructor name when the class name changes. For backward compatibility, if there is no method named __construct() in a class, PHP will search for a constructor method written in php4 with the same name as the class name. Format: function __construct ([parameter]) { ... ... } Only one constructor method can be declared in a class, but the constructor method will only be called once every time an object is created. This method cannot be called actively, so usually Use it to perform some useful initialization tasks. For example, the corresponding properties are assigned initial values ​​when the object is created.​ 1. //Create a human 2. 3. 0class Person 4. 0{ 5. //The following are the member attributes of people 6. var $name; //The person’s name 7. var $sex; //The gender of a person 8. var $age; //Age of person 9. //Define a constructor parameter as name $name, gender $sex and age $age 10. function __construct($name, $sex, $age) 11. { 12. //The $name passed in through the construction method assigns an initial value to the member attribute $this->name 13. $this->name=$name; 14. //The $sex passed in through the construction method assigns an initial value to the member attribute $this->sex 15. $this->sex=$sex; 16. //The $age passed in through the construction method assigns an initial value to the member attribute $this->age 17. $this->age=$age; 18. } 19. //This person’s way of speaking 20. function say() twenty one. { 22. echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."say(); 31. //The following accesses the speaking method in the $p2 object. 32. $p2->say(); 33. //The following accesses the speaking method in the $p3 object. 34. $p3->say(); ​ The output result is: My name is: Zhang San 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767257.htmlTechArticleThe declaration of the PHP constructor is the same as the declaration of other operations, except that 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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!