The PHP class initialization function plays an important role in PHP, and this article will explain the content of its related code.
<!--? PHP class Shopproduct {public $title = "Default Product"; Public $producerMainName = "main name"; Public $producerFirstName = "First name"; Public $price = 0; function Getproducer () { return ' {$this--->producerfirstname} '. "{$this->producermainname}"; } $product 1 = new shopproduct (); $product 1->title = "My Antonia"; $product 1->producermainname = "Cather"; $product 1->producerfirstname = "Willa"; $product 1->price = 5.99; Print "Author: {$product 1->getproducer ()}";>
<!--? PHP class Shopproduct {public $title; public $producerMainName; public $producerFirstName; Public $price = 0; function __construct ($title, $firstName, $mainName, $price) {//Construction method $this--->title = $title; $this->producerfirstname = $firstName; $this->producermainname = $mainName; $this->price = $price; } function Getproducer () { return ' {$this->producerfirstname} '. "{$this->producermainname}"; } } $product 1 = new Shopproduct ("My Antionia", "Willa", "Cather", 5.99); Print "Author: {$product 1->getproducer ()}";>
The previous initialization functionality is integrated into the class to reduce duplication of code. The __construct () method is called when an object is created with the new operator.
This article explains the PHP class initialization function code content, more relevant knowledge, please pay attention to the PHP Chinese web.