Blogger Information
Blog 31
fans 0
comment 2
visits 27453
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月8日作业——使用use 来导入空间/别名
钱光照的博客
Original
719 people have browsed it

一、use 来导入空间/别名

1.创建具有命名空间的Family类 

代码如下:Family.php

实例

<?php
namespace Family;
 /**
 * 创建 Family类
 */
class Family
{
    protected $familyName= '';
    protected $familyAddr = '';
    protected $postCodes = '';
    const COMPANY = '严桥中学';


    //构造方法
    public function __construct($familyName='温馨之家',$familyAddr='安徽省无为县严桥镇', $postCodes='238381') 
    {
        //如果传参,则使用新值初始化属性,否则使用默认值
        $familyName ? ($this->familyName = $familyName) : $this->familyName;
        $familyAddr ? ($this->familyAddr = $familyAddr) : $this->familyAddr;
        $postCodes ? ($this->postCodes = $postCodes) : $this->postCodes;                
    }
    
    //查询器
    public function __get($attribute)
    {
        return $this->$attribute;
    }
    
    //设置器
    public function __set($attribute,$value)
    {
        return $this->$attribute = $value;
    }
    
    //在类中访问类常量,使用self来引用当前类名
    public function getConst()
    {
        //类内部也可以直接使用当前类名
//        return Family::COMPANY;
        //推荐使用self:当类名改变时,不必修改内部对它的引用
        return self::COMPANY;
    }
    public function where(){
    return $this->familyAddr;
    }
}

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


2.  创建具有命名空间的Member类  Member.php

实例

<?php
namespace Member;
require 'Family.php';
/**
 * 创建成员类:Person1
 */
class Member
{
     //属性声明
    private $name='';// 姓名
    private $age=0;// 年龄
    private $hobby=[];// 爱好
    private $relation='';// 关系
    private $data=[];//自定义数据收集器


    //构造方法
    public function __construct($name='',$age=0,array $hobby=[],$relation='' )
    {
        $this->name=$name;
        $this->age=$age;
        $this->hobby=$hobby;
        $this->relation=$relation;
    }
    
    //魔术方法:查询器    
    public function __get($attribute)
    {
        $msg=null;
        if(isset($this->$attribute)){
            $msg= $this->$attribute;            
        }elseif(isset ($this->data[$attribute])){
            $msg= $this->data[$attribute];            
        }else{
            $msg='无此属性';
        }
        return $msg;
    }
    
    //魔术方法:设置器
    public function __set($attribute,$value)
    {
        if(isset($this->$attribute)){
            $this->$attribute=$value;            
        }else{
            $this->data[$attribute]=$value;//如果属性不存在,创建它并保存到类属性$data数组中
        }
    }
        
}
$member1 = new Member('钱天行',12,['看书','学习']);
echo $member1->name;
use Family\Family;
$family1 = new Family();
echo '出生在'.$family1->familyName.',他喜欢'.$member1->hobby[0].'和'.$member1->hobby[1].'!';

运行实例 »

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


二、运行结果


1.png

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