Blogger Information
Blog 38
fans 0
comment 1
visits 30393
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月2日创建类和对象,魔术方法
1
Original
1111 people have browsed it

实例

<meta charset="UTF-8">
<?php
class GirlFriend
{
    //创建私有成员
    private $name;
    private $age;
    //构造器,用于实例化初始化
    public function __construct($name,$age)
    {
        $this->name = $name;
        $this->age = $age;
    }
    //getname方法
    public function  getname()
    {
        return $this->name;
    }
    //getage方法
    public function  getage()
    {
        return $this->age;
    }
    //没有魔术方法就要这样
//    public function setname($value)
//    {
//        return $this->name = $value;
//    }
    //魔术方法,用于获取方法
    public function __set($name, $value)
    {
      return $this->$name = $value;
    }
//魔术方法,用于获取私方法
    public function __get($name)
    {
       return $this->$name;
    }
}
//实例化
$demo1 = new GirlFriend('叶筱葵',27);

echo $demo1->getname();
echo $demo1->getage();

$demo1->name = '雪贞';
echo $demo1->name;

运行实例 »

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


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
  • 1
    2018-03-16 00:39:40
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!