Blogger Information
Blog 22
fans 1
comment 1
visits 22660
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php对象之构造方法,魔术方法
forever浅笑
Original
851 people have browsed it

111.gif

实例

<?php

class Person {

	private $name = '';
	private $sex = '';
	private $old = 0;
	private $martical = '';

	public function __construct($name, $sex, $old, $martical) {
		$this->name = $name;
		$this->sex = $sex;
		$this->old = $old;
		$this->martical = $martical;
	}

	public function show() {
		echo '<b>姓名:</b> ' . $this->name . ' ' . '<b>性别:</b> ' . $this->sex . ' ' . '<b>年纪:</b> ' . $this->old . ' ' . '<b>武功:</b> ' . $this->martical;
	}
	
	public function __set($name, $value) {
		$this->$name = $value;
	}
	
	public function __get($name) {
		return $this->$name;
	}

}

$wxb = new Person('韦小宝', '男', '20', '三寸不烂之舌');
$wxb->show();
echo '<hr>';
$qf = new Person('乔峰', '男', '30', '降龙十八掌');
$qf->show();
echo '<hr>';
$zwj = new Person('张无忌', '男', '25', '九阳神功');
$zwj->show();
echo '<hr>';
$xz = new Person('虚竹', '男', '22', '北冥神功');
$xz->show();
echo '<hr>';
$yg = new Person('杨过', '男', '24', '九阴真经');
$yg->show();
$yg->martical = '除了九阴真经还有自创的黯然消魂掌';
echo '<hr>';
$yg->show();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!