Blogger Information
Blog 46
fans 3
comment 1
visits 33378
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
502 php类的构造与查询设置
吃不起土的少年的博客
Original
692 people have browsed it

实例

<?php
/**
 * Created by PhpStorm.
 * User: 金超
 * Date: 2018/5/3
 * Time: 15:43
 */
 namespace movieInfo;
class MovieList{
    private $name='';
    private $size='0 byte';
    private $CreationDate='0000-00-00';
    private $charge=[];
    private $data=[];
   //构造
    public function __construct($name='',$size='',$CreationDate='',array $charge=[])
    {
        $this->name=$name;
        $this->size=$size;
        $this->CreationDate=$CreationDate;
        $this->charge=$charge;

    }

    //查询器
    public function __get($name)
    {
        $warning=null;
        if(isset($this->$name)){
        $warning=$this->$name;

        }elseif(isset($this->data[$name])){
            $warning=$this->data[$name];

        }else{
            $warning='未查到该属性';
    }
    return $warning;


    }
    //设置器
    public function __set($name, $value)
    {
        if(isset($this->$name)){
            $this->$name=$value;


        }else{
            $this->data[$name]=$value;
        }
        // TODO: Implement __set() method.
    }
}

运行实例 »

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

测试函数

实例

<?php

require 'class/movielist.php';

$movielist=@new \movieInfo\MovieList('碟中谍','199,121,254 byte','2016-04-27',['no',0]);

echo'影片名:'.$movielist->name.'<br>';
echo'影片大小:'.$movielist->size.'<br>';
echo'更新日期:'.$movielist->CreationDate.'<br>';
echo'是否收费:'.print_r($movielist->charge[0],true).'费用:'.print_r($movielist->charge[1],true).'元'.'<br>';


echo'<hr style="color:orangered">';

//测试__set()
$movielist->name='泰坦尼克号';
$movielist->size='279,316,484 byte';
$movielist->CreationDate='1998-02-15';
$movielist->charge=['yes','$25'];


echo'影片名:'.$movielist->name.'<br>';
echo'影片大小:'.$movielist->size.'<br>';
echo'更新日期:'.$movielist->CreationDate.'<br>';
echo'是否收费:'.print_r($movielist->charge[0],true).'费用:'.print_r($movielist->charge[1],true).'元'.'<br>';

echo'<hr style="color:#7a43b6">';

//用户自定义属性
$movielist->reward='第70届奥斯卡金像奖';
$movielist->link='http://www.iqiyi.com/v_19rrny4w8w.html';
$link=$movielist->link;
echo'获奖:'.$movielist->reward.'<br>';
echo'观看地址:'."<a href={$link} title=''>".$movielist->link.'<br>';

运行实例 »

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

测试结果

QQ截图20180503181201.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