Blogger Information
Blog 36
fans 4
comment 3
visits 31563
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
7.31 php命名空间、类的继承与实现
大灰狼的博客
Original
1466 people have browsed it

对php命名空间简单理解

PHP 命名空间可以解决以下两类问题:

用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲突。

为很长的标识符名称(通常是为了缓解第一类问题而定义的)创建一个别名(或简短)的名称,提高源代码的可读性。

在编程中每个类、函数等都有自己的名字以备调用、名字多了重复很常见。但重复就会出错,为了不重复就只能设置冗余过长的名字,因此命名空间就很好解决。就好像电脑硬盘 C盘 D盘 F盘 你可以在C盘有个dahuilang文件夹 也可以在D盘有个dahuilang 一级分类不满足 还可以二级分类 dahuilang\xiaohuilang   只要使用时候告诉程序 我是C盘dahuilang  他是D盘dahuilang即可区分 这样大大提高了名字的使用率还不会乱。利于团队合作 项目拓展。

===========================================分割线

类的继承与实现

简单小案例

1.jpg


实例

<?php
//命名空间
namespace User;
// 类注册用户
class Reg
{
	//实例属性
	public $name;//名字
	public $mobile;//手机号
	public $uid;//用户id
	public $level;//用户等级
	public function __construct($name,$mobile){
		$this->name=$name;
		$this->mobile=$mobile;
		$this->uid=$uid=rand(1,1000);
		$this->level='注册会员';
	}
	public function getId(){
		return ['uid'=>$this->uid,'name'=>$this->name,'mobile'=>$this->mobile,'level'=>$this->level];

	}
	
}
//子类Complete注册用户完善信息 继承 Reg类
class Complete extends Reg
{
	//实例属性
	public $sfzId;//***号
	public $email;//邮箱
	public function __construct($name,$mobile,$sfzId,$email){
		parent::__construct($name,$mobile);
		$this->sfzId=$sfzId;
		$this->email=$email;
		$this->level='认证会员';
	}
	public function getId(){
		return ['uid'=>$this->uid,'name'=>$this->name,'mobile'=>$this->mobile,'sfzId'=>$this->sfzId,'email'=>$this->email,'level'=>$this->level];
		
	}
}
//用户充值根据充值金额设置不同用户级别
class Pay extends Complete
{
    public $money;//充值金额
    public $balance;//账户余额
    public function __construct($name, $mobile, $sfzId, $email,$money,$balance)
    {
        parent::__construct($name, $mobile, $sfzId, $email);
        $this->money=$money;
        $this->balance=$balance+$money;
    }
    public function pay(){
        switch (true) {
        	case $this->money>=10000 && $this->money<=29999 :
        		$this->level='铜牌用户';
        		break;
        	case $this->money>=30000 && $this->money<=49999 :
        		$this->level='银牌用户';
        		break;
			case $this->money>=50000 && $this->money<=79999 :
        		$this->level='金牌用户';
        		break;
			case $this->money>=80000 :
        		$this->level='铂金用户';
        		break;		
        	default:
        		$this->level<=0;
				return '充值金额不允许小于等于0';
        		break;
        }
		return ['uid'=>$this->uid,'name'=>$this->name,'mobile'=>$this->mobile,'sfzId'=>$this->sfzId,'email'=>$this->email,'level'=>$this->level,'money'=>$this->money,'balance'=>$this->balance];

    }
}



//注册用户
$user1=new Reg('刘晓飞','13856568585');
echo '<pre>'.print_r($user1,true);

echo $user1->uid.' ';
echo $user1->level.' ';
echo $user1->name.' ';
echo $user1->mobile.' ';
echo '<hr>';


//注册用户完善信息 完成身份认证
$user2=new Complete('王师傅','13542327777','610555198022223434','php@qq***');

echo '<pre>'.print_r($user2->getId());

echo '<hr>';

//用户进行充值 会员级别改变

$user3=new Pay('大灰狼','13868689898','610122199901011234','php@qq***',60000,1585);


echo '<pre>'.print_r($user3->pay(),true);

echo '<hr>';
//查看类中的属性
//$properties = get_class_vars(Pay::class);
//echo '<pre>' . print_r($properties, true);

// 查看类中方法
//$methods = get_class_methods(Pay::class);
//echo '<pre>' . print_r($methods, true);

运行实例 »

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


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!