Blogger Information
Blog 18
fans 0
comment 0
visits 13531
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对命名空间和类的继承案例 2019年07月31日
高明博客
Original
1296 people have browsed it

第一、命名空间

抽象概念:

什么是命名空间?从广义上来说,命名空间是一种封装事物的方法。在很多地方都可以见到这种抽象概念。例如,在操作系统中目录用来将相关文件分组,对于目录中的文件来说,它就扮演了命名空间的角色。

具体举个例子,文件 footer.php 可以同时在目录/home/greg 和 /home/other 中存在,但在同一个目录中不能存在两个 footer.php  文件。另外,在目录 /home/greg 外访问 footer.php 文件时,我们必须将目录名以及目录分隔符放在文件名之前得到 /home/greg/ footer.php。这个原理应用到程序设计领域就是命名空间的概念。

 

个人理解概念:

针对命名空间个人理解是,PHP程序设计中,通过使用命名空间,可划分不同的代码功能区域区,可以很大程度避免代码重复和方法重名问题,既能实现代码相连互通,又能实现相互调用。

 

命名空间案例:


<?php
// 函数:  命名函数, 匿名函数
// 空间:  命名空间, 匿名空间(全局空间)

//命名空间
//全局中,不允许有重名的成员

//例如:声明常量NAME,名称不能重复
//const NAME = 'Joe';
//const NAME = 'Joe';

 

//分别定义3个命名空间 well,good,righto

namespace well
{
     //定义命名空间well下面的action类,且在该类下定义show方法
     class action{
         public function show(){
             echo "好";

         }
     }

    //::class,可以获取到当前类完整的类名称
    //完整的类名称,包括类名和它所在的命名空间的名称

    //测试举例:
    echo action::class;
    echo "<br>";

}

namespace good
{
    //定义命名空间good下面的action类,并访问其他命名空间中的类方法
    class action{ }
    //调用show命名空间里action类的show方法
    (new \righto\action())->show();

    //调用well命名空间里action类的show方法
    (new \well\action())->show();

}


namespace righto
{
    //定义命名空间righto下面的action类
    class action{
        public function show(){
            echo "非常好"."<br>";
            echo  __METHOD__.'<br>';
        }
    }

}

本地运行截图

mmkj.png

 

第二、类的继承与实现案例

上代码


<?php

//举实例演示类的继承与实现

//定义一个命名空间fruits(水果)
namespace fruits;

//定义类Shu
class Shu{

    //定义属性,名称、单价、斤数
    public $name;
    public $price;
    public $jin;

    //构造函数
    public  function __construct($name ='苹果',$price=30,$jin=5)
    {
        $this-> name =$name;
        $this->price = $price;
        $this->jin =$jin;
    }

    //实例方法 getInfo()
    public  function getInfo(){
        return '水果名称:'.$this->name.'<br>水果单价:'.$this->price.'<br>水果斤数:'.$this->jin.'<br>';
    }

    //析构函数
    public function __destruct()
    {
//        echo '<h3 style="color: red">执行结束</h3>';
    }

}

//类实例化对象
$obj = new Shu();

//用实例化对象调用getInfo方法
echo $obj->getInfo();

$obj = null;//unset($obj);

//创建子类Get1,类的继承,实现代码复用

class Get1 extends Shu{
    /**
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }
}

$get1 = new Get1('香蕉',5,3);
echo $get1->getInfo();

//创建子类Get2,扩展父类,增加属性或方法

class Get2 extends Shu{

    public  $user;  //客户名

    //子类构造方法
    public function __construct($user,$name = '苹果', $price = 30, $jin = 5)
    {
        parent::__construct($name, $price, $jin);

        //自定义属性的初始化
        $this->user = $user;
    }

    //添加一个方法,计算总价

    public function total(){

        return round($this->price*$this->jin,2);
    }
}

//实例化子类
$get2 = new Get2('Joe','榴莲','20','3');
echo $get2->user.'客户,***了'.$get2->jin.'斤'.$get2->name.',单价是'.$get2->price.',合计消费:'.'<span style="color: red;font-size: 40px">'.$get2->total().'</span>'.'元。<br>';


//水果店促销:根据用户***水果金额不同,给与不同价格折扣

class Get3 extends Get2{

    //将父类Get2中的total()方法进行重写
    public  function total()
    {
       //获取当前用户的消费金额
        $total = parent::total();
        //设置折扣率

        //switch:用区间判断,所以传入一个true
        switch (true){
            case ($total >= 30 && $total <=50):
                $discountRate = 0.8;
                break;
            case ($total >50 && $total <100):
                $discountRate = 0.7;
                break;
            case ($total >100):
                $discountRate = 0.6;
                break;
            default://小于30元,不打折扣
                $discountRate = 1;
        }

        $discountPrice = round($total*$discountRate,2);
        //如果折扣小于1,给折扣描红输出
        if($discountRate < 1)
        {
            $discountPrice = $discountPrice.'元,<span style="color: red">('.$discountRate.'折)</span>';

        }

        return $discountPrice;
    }

}


//用户名,苹果名,单价,斤数
$get3 = new Get3('小明','苹果','10','5');
echo $get3->user.'客户,***了'.$get3->jin.'斤'.$get3->name.',单价是'.$get3->price.',合计折扣消费:'.'<span style="color: red;font-size: 40px">'.$get3->total().'</span>'.'元。<br>';
$get3 = new Get3('小芳','山楂','7','12');
echo $get3->user.'客户,***了'.$get3->jin.'斤'.$get3->name.',单价是'.$get3->price.',合计折扣消费:'.'<span style="color: red;font-size: 40px">'.$get3->total().'</span>'.'元。<br>';
$get3 = new Get3('小宇','山竹','20','10');
echo $get3->user.'客户,***了'.$get3->jin.'斤'.$get3->name.',单价是'.$get3->price.',合计折扣消费:'.'<span style="color: red;font-size: 40px">'.$get3->total().'</span>'.'元。<br>';

 

本地运行截图

hsjc.png

 

 

总结

  1. 定义命名空间,例如:namespace _0731;全局中,不允许有重名的成员

  2. ::class,可以获取到当前类完整的类名称(完整的类名称,包括类名和它所在的命名空间的名称

  3. //调用show命名空间里action类的show方法:
    (new \righto\action())->show();

  4. 客户端代码: 凡是调用了函数/类的代码 例如:$obj = new Demo3();

  5. get_class_vars(): 返回类中属性组成的数组

  6. 访问实例方法getInfo1()  代码是:echo $obj->getInfo1();

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

  8. // 子类Sub1, 继承Demo5父类,代码复用,示例代码
    class Sub1 extends Demo5
    {
        // ...
    }

Correction status:qualified

Teacher's comments:你的第一段话, 是直接从php.net官网上抄过来
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!