当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP类初始化功能代码

PHP类初始化功能代码

2017年12月22日  | 移动技术网IT编程  | 我要评论
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()方法会被调用。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网