欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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()方法会被调用。