PHP类初始化功能代码
程序员文章站
2022-07-02 19:01:49
PHP类初始化功能代码
producerFirstName}" . " {$this->producerMainName}";
}...
PHP类初始化功能代码
producerFirstName}" . " {$this->producerMainName}"; } } $product1 = new ShopProduct(); $product1->title = "My Antonia"; $product1->producerMainName = "Cather"; $product1->producerFirstName = "Willa"; $product1->price = 5.99; print "author: {$product1->getProducer()}"; >
----------------------------------------------------------------------
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()}"; >
上面把之前的初始化功能集成到类中,以减少代码的重复。当使用new操作符创建对象时,__construct()方法会被调用。
下一篇: 二货小年轻,笑你肚子疼
推荐阅读