Blogger Information
Blog 34
fans 2
comment 2
visits 22732
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的方法 20180503-16:56
AsNwds_MIS王的博客
Original
695 people have browsed it

创建类的实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 15:17
 */

class conn
{ //声明类属性
//    public $name= '王先生';
//
//    public $city = '鞍山';
//
//    public $like =['吃','喝','玩','乐'];
    public $name ='';

    public $city ='';

    public $like =[];

  //  构造方法,对象属性的初始化
    public function __construct($name ,$city ,array $like)
    {
        $this->name = $name;
        $this->city = $city;
        $this->like = $like;
    }

    //查询器 __get()
    public function __get($city)
    {
        // TODO: Implement __get() method.
        return $this->$city;
    }

    //设置器 __set()
    public function __set($city, $value)
    {
        // TODO: Implement __set() method.
        if (isset($this->$city))
            {
                $this->$city =$value;
            } else {
            $this->data[$name] = $value;
        }
    }

}

运行实例 »

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

调用类

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 15:34
 */
//导入类
require './class/conn.php';

//创建类的实例
//$conn =new conn();
//测试


$conn =new conn('王','鞍',['吃','喝','玩','乐']);

//var_dump($conn);


$conn->city ='山';

echo $conn->city;

运行实例 »

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

总结:  类是对象的模板,对象是类的实例

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