Blogger Information
Blog 48
fans 0
comment 0
visits 36355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类实例化,访问私有属性和公有属性-2018.08.29
雨天的博客
Original
1312 people have browsed it

实例

<?php
/**
 * 1.对象的创建
 * 2.对象成员的初始化
 * 3.对象成员的访问
 */
class Person{
    public $name;
    public $age;
    private $salary;
    private $weight;
    function __construct($name,$age,$salary,$weight)
    {
        $this->name = empty($name) ? '' : $name;
        $this->age = empty($age) ? '没有填写年龄' : $age;
        $this->salary = $salary;
        $this->weight = $weight;
    }
    function  __get($name)
    {
        $msg='无此属性';
        if(!empty($this->$name))
        {
            $msg=$this->$name;
        }
        return $msg;

    }
    function  __set($name,$value)
    {
        $this->$name = $value;
    }
}
$person = new Person('Tom',22,3000,'');
echo '姓名:'.$person->name,'<br>';
echo '年龄:'.$person->age,'<br>';
echo '体重:'.$person->weight,'<br>';
echo '工资:'.$person->salary;

运行实例 »

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


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!