Blogger Information
Blog 42
fans 0
comment 1
visits 26002
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的构造方法及查询器、设置器学习-2018年5月3日晚19:00完成
邵军-山东-84918的博客
Original
551 people have browsed it

类代码:

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3 0003
 * Time: 下午 4:52
 */

class Computer
{
private $name;
private $price;
private $dress;
private $data=[];

public function __construct($name='',$price=0,$dress=''){
$this->name=$name;
$this->price=$price;
$this->dress=$dress;
}
public function __set($name,$value){
    return isset($this->$name)?$this->$name = $value:$this->data[$name] = $value;
}
public function __get($name){
    $msg=null;
if(isset($this->$name)){
    $msg=$this->$name;
    }elseif (isset($this->data[$name])){
    $msg=$this->data[$name];
    }else{
    $msg ='不存在该数据!';
    }
    return $msg;
}
}

运行实例 »

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

前端代码:

实例

<?php
require 'Computer.php';
$Computer=new Computer('dell',7000,'usa');
//魔术方法__get
echo '品牌:',$Computer->name,'<br>';
echo '价格:',$Computer->price,'<br>';
echo '产地:',$Computer->dress,'<br>';
echo '保修:',$Computer->repair;
echo '<hr>';
//魔术方法__set
$Computer->name='asus';
$Computer->price=6000;
$Computer->dress='China';

echo '品牌:',$Computer->name,'<br>';
echo '价格:',$Computer->price,'<br>';
echo '产地:',$Computer->dress,'<br>';
$Computer->color='black';
echo '颜色:',$Computer->color,'<br>';
echo '颜色:(自定义属性)',print_r($Computer->data);

运行实例 »

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


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