Explain PHP class initialization function code

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.