PHP类初始化功能代码
程序员文章站
2022-03-29 21:05:07
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()方法会被调用。
上一篇: javascript获取select标签选中的值讲解
下一篇: python函数高级用法教程