Blogger Information
Blog 31
fans 3
comment 1
visits 34320
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php构造方法、查询器、设置器
php学习笔记
Original
1028 people have browsed it

1.创建一个类文件Programmer.php

<?php
class Programmer
{
	private $name = '';
	private $age = 0;
	private $language = '';
	
	public function __construct($name='',$age=0,$language='')
	{
	$this->name = $name;
    $this->age = $age;
	$this->language = $language;
	}
	
	public function __get($name)
	{
	$msg = null;
    if(isset($this->$name))
	{
		$msg = $this->$name;
	}else{
	   $msg = '无此属性';
	}
    return $msg;	
	}
	
	public function __set($name,$value)
	{
		$this->$name = $value;
	}
}

2.创建一个php脚本进行调用

<?php
include 'class/Programmer.php';
$programmer1 = new Programmer('大神',24,'php');
//测试查询器__get()
echo '姓名:'.$programmer1->name .'<br>';
echo '年龄:'.$programmer1->age .'<br>';
echo '编程语言:'.$programmer1->language .'<br>';
echo '<hr>';
//测试设置器__set()
$programmer1->name = '肖奈';
$programmer1->age = 26;
$programmer1->language ='python';
echo '姓名:'.$programmer1->name .'<br>';
echo '年龄:'.$programmer1->age .'<br>';
echo '编程语言:'.$programmer1->language .'<br>';

运行结果:

运行结果.png


Correction status:qualified

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