PHP class initialization function plays an important role in PHP. This article will explain the content of its related code in detail.
<!--?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}"; } $product1 = new ShopProduct(); $product1->title = "My Antonia"; $product1->producerMainName = "Cather"; $product1->producerFirstName = "Willa"; $product1->price = 5.99; print "author: {$product1->getProducer()}"; >
<!--?php class ShopProduct { public $title; public $producerMainName; public $producerFirstName; public $price = 0; function __construct($title,$firstName,$mainName,$price) { //构造方法 $this--->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } function getProducer() { return "{$this->producerFirstName}" . " {$this->producerMainName}"; } } $product1 = new ShopProduct( "My Antionia", "willa", "Cather", 5.99); print "author: {$product1->getProducer()}"; >
The previous initialization function is integrated into the class to reduce code duplication. When an object is created using the new operator, the __construct() method is called.
This article explains the content of PHP class initialization function code. For more related knowledge, please pay attention to the PHP Chinese website.
Related recommendations:
Explain PHP object-oriented, PHP inheritance related code
Explain PHP object-oriented serialization and deserialization Related code
How to determine whether it is a mobile login through PHP method (code)
The above is the detailed content of Explain the PHP class initialization function code. For more information, please follow other related articles on the PHP Chinese website!