Blogger Information
Blog 14
fans 1
comment 0
visits 4548
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
详说include, require的区别与联系, 与当前脚本的作用域的关系以及类与对象的常用关键字,操作,以及几个魔术方法的演示
叫我孙大树
Original
320 people have browsed it
<?php
//include, require的区别与联系, 与当前脚本的作用域的关系
//include,require最重要的区别就是:include在载入文件时如果出现错误,会报错,但代码还会正常运行下去;require在载入文件时如果出现问题将会直接终止代码运行。
//因此,include适合载入不太重要的外部文件,require适合载入重要的某种底层文件。它们与当前脚本共用一个作用域。

//将课堂上演示的类与对象的常用关键字,操作,以及几个魔术方法进行演示
class Competitor
{
    //选手姓名
    public $name;
    //选手年龄
    public $age;
    //选手组别
    public $group;
    //选手性别
    protected $isMale;
    //随机一个静态属性
    public static $count;

    public function __construct(string $name, int $age, string $group, bool $isMale)
    {
        $this->age = $age;
        $this->name = $name;
        $this->group = $group;
        $this->isMale = $isMale;
        self::$count += 1;
    }

    public function getSex()
    {
        return $this->isMale;
    }

    public static function getCount()
    {
        return self::$count;
    }

    public function getInfo()
    {
        $arr = [
            'age' => $this->age,
            'name' => $this->name,
            'group' => $this->group,
            'isMale' => $this->isMale,
        ];
        return $arr;
    }

    public function __get($value)
    {
        if (isset($this->$value) && $value != 'isMale') {
            return $this->$value;
        } else {
            return 'Unknow Value';
        }
    }

    public function __set($name, $value)
    {
        $this->$name = $value;
    }
}

$competitor = new Competitor('叫我孙大树', 23, '青年组', true);
print_r($competitor->getInfo());
echo '<br>';
$competitor->aaaa = 666;
echo $competitor->aaaa . '<br>';
echo Competitor::$count . '<br>';
$test = new Competitor('test', 2, 'test', false);
echo Competitor::$count . '<br>';
echo $competitor->isMale . '<br>';
echo $competitor->getSex() ? '性别是:男<br>' : '性别是:女<br>';
echo $test->getSex() ? '性别是:男<br>' : '性别是:女<br>';

运行实例 »

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


Correcting teacher:PHPzPHPz

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