Blogger Information
Blog 27
fans 0
comment 0
visits 17797
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类-9.03
Yyk.的博客
Original
558 people have browsed it

类声明与类的实例化

实例

<?php
header("Content-Type:text/html;charset=utf-8");

//class声明
class Demo1
{
	public $name = 'Y';
	public $salary = 3000;
	
	protected $sex = 0;
	
	private $age = 19;
	
	public function getSex(){
		return ($this->sex ==0) ? '男' : '女';
	}
	
	public function getAge(){
		return ($this->sex ==0) ? $this->age.'岁' : '无权查看';
	}
	
}

//new进行实例化
$demo1 = new Demo1();

echo $demo1->name.'的工资是'.$demo1->salary.'<br>性别为'.$demo1->getSex().',年龄为'.$demo1->getAge();




?>

运行实例 »

 类常量与类属性的重载

实例

<?php
header("Content-Type:text/html;charset=utf-8");

//class声明
class Demo
{

	private $name;
	private $course;
	private $grade;
	
	//构造器构造方法
	public function __construct($name,$course,$grade){
		$this->name = $name;
		$this->course = $course;
		$this->grade = $grade;
	}
	
	public function show(){
		 return $this->name.'的《'.$this->course.'》课程的成绩是: '.$this->grade.'分!';
	}
	
	
	
	//获取属性的重载
	public function __get($x){
		if($x == 'grade'){
			return '不允许查看';
		}
		return $this->name;
	}
	
	
	//更新属性的重载
	public function __set($name,$value){
		if($name == 'grade'){
			echo '不允许修改';
		}
		$this->$name = $value;
	}
	
	//属性重载的检测
	public function __isset($name){
		if($name == 'grade'){
			return false;
		}
		return isset($this->name);
	}
	
	//销毁属性的重载
	public function __unset($name){
		if($name == 'grade'){
			return false;
		}
		unset($this->$name);
	}

}

$demo = new Demo('Y','PHP', 82);

echo $demo->grade;
echo '<hr>';

$demo->name = 'YY';
echo $demo->name;
$demo->grade = 60;
echo $demo->grade;

echo '<hr>';
echo isset($demo->grade) ? 'yes' :'no';
echo isset($demo->name) ? 'yes' :'no';

echo '<hr>';
unset($demo->grade);
echo $demo->grade;
unset($demo->name);
echo $demo->name;






?>

运行实例 »

类的继承与方法重写

实例

<?php
header("Content-Type:text/html;charset=utf-8");


//父类
class Demo
{
	public $name;
	public $age;
	public $salary;
	const APP_NAME = '学生管理系统';
	
	public function __construct($name,$age)
	{
		$this->name = $name;
		$this->age = $age;
		
	}
	
	public function __get($name)
	{
		if(isset($this->$name)){
			return $this->$name;
		}
		return '非法属性';
		
	}
	
}

//子类
class Demo_1 extends Demo
{
	private $sex;
	const APP_NAME = '教师管理系统';
	
	public function __construct($name,$age,$sex='male'){
		parent::__construct($name,$age);
		$this->sex = $sex;
	}
	
	public function __get($name){
		if(isset($this->$name)){
			return $this->$name;
		}
		return '非法属性';
			
	}
	
}

$demo = new Demo('Y',23);
$demo1 = new Demo_1('Q',24);

echo Demo::APP_NAME,'<hr>';
echo Demo_1::APP_NAME,'<hr>';

echo $demo->age,'<hr>';
echo $demo1->age,'<hr>';

echo $demo1->sex;








?>

运行实例 »

类中静态成员的声明与访问

实例

<?php
header("Content-Type:text/html;charset=utf-8");


class Demo
{
	public static $pdo = null;
	protected static $db = ['type'=>'mysql',
							'host'=>'127.0.0.1',
							'dbname'=>'php',
							'user'=>'root',
							'pass'=>'root'];
	//连接数据库
	public static function connect(){
		$dsn = self::$db['type'].':host='.self::$db['host'].';dbname='.self::$db['dbname'];
		self::$pdo = new PDO($dsn,self::$db['user'],self::$db['pass']);
	}
	
	public static function select($table,$field='*',$salary,$num=5){
		$stmt = self::$pdo->prepare("SELECT {$field} FROM {$table} WHERE `salary`>{$salary} LIMIT {$num}");	
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
}
Demo::connect();
$result = Demo::select('admin','id,name,QQ,salary',4000,5);
echo '<pre>';
var_export($result);





?>

运行实例 »

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


总结:

属性重载:
构造器:  __construct   --》new demo(‘a’,'b)
获取:    __get       --》$demo->name
设置(更新):    __set     -->$demo->name='b'
检测:   __isset()  -->isset()
销毁:    __unset   --unset('$demo->name)


访问类中的const
echo Demo::APP_NAME;


类中静态成员不用创建(new不用)



今天开始补作业!!!!!!!!!!



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