Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the php construction method

Shulou Source: shulou.com Published: 2022-06-01 13:10:53 09月16日 Update

This article is about how to use the php constructor. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Php construction method

The php constructor, also known as the constructor, is automatically executed when the object is instantiated.

Syntax:

Function _ _ construct () {/ / two dashes

}

Code example:

Class Student {private $name; private $sex; public function _ _ construct () {echo 'this is the constructor';} / / the method with the same name as the class name is the constructor. It is not recommended to use public function Student () {}} stu1 = new Student;class Student {private $name; private $sex in PHP Public function _ _ construct ($name,$sex) {$this- > name = $name; $this- > sex = $sex;} public function show () {echo 'name:'. $this- > name; echo 'gender:'. $this- > sex;}} $stu1 = new Student ('Qingyu', 'male'); $stu1- > show () Thank you for reading! This is the end of this article on "how to use the php construction method". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Method content more article good practical two code function name instance object suggestion gender article time see knowledge example class name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Redmi NVidia vpn