Blogger Information
Blog 34
fans 1
comment 0
visits 23086
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php类的基础介绍与实战_2018年9月3号
theYon的博客
Original
531 people have browsed it

php类的基础介绍与实战

主要知识点

1)类的声明与实例化

a.类声明: class

b.类的实例化: new

c.对象成员的访问:->

2)对象的三大特征: 封装,继承,多态
3)类常量,对象初始化,属性的重载
4)类中的静态成员与访问

代码

<?php

 // 类的声明
class Animal
{
    // 类常量
    const EARTH = '地球';
    const MARS = '火星';

    private $name;
    protected $type;
    protected $nickName;

    // 构造器
    public function __construct($name='动物',$type='类型',$nickName='昵称')
    {
        $this->name = $name;
        $this->type = $type;
        $this->nickName = $nickName;
    }

    // 属性的更新重载
    public function __set($name,$value) {
        if ($name == 'name') {
            echo $name.'不允许修改','<hr>';
            return false;
        }
        $this->$name = $value;
    }

    // 属性的获取重载
    public function __get($name) {
        if(isset($this->$name)){
            if($name == 'type') {
                echo $name.'不允许查看','<br>';
                return false;
            }
            return $this->$name;
        }
        return 'father非法属性';
    }

    // 属性方法
    public function run(){
        echo $this->nickName.'在跑';
    }
}

// 继承Animal类
class Cat extends Animal
{
    // 重写了EARTH
    const EARTH = '地球cat';

    private $feed;

    // 静态属性
    public static $firstName;
    private static $lastName;

    public function __construct($name='动物cat',$type='类型cat',$nickName='昵称cat',$feed='fish',$firstName='jack',$lastName='BBB')
    {
        parent::__construct($name,$type,$nickName);
        $this->feed = $feed;
        self::$firstName = $firstName;
        self::$lastName = $lastName;
    }

    // 将父类属性重载方法重写
    public function __get($name) {
        if(isset($this->$name)){
            return $this->$name;
        }
        // 不可这样设置
        // if(isset(self::$name)){
        //     return self::$name;
        // }
        return 'son非法属性';
    }

    // 将父类属性方法重写
    public function run(){
        echo $this->nickName.'在吃'.$this->feed;
    }

    public static function sleep(){
        echo self::$firstName.'-'.self::$lastName.'在睡觉zZ';
    }
}

$cat = new Cat('cat','波士猫','喵喵怪','apple','emm');
$cat->name = 'none';

echo $cat::EARTH.'<hr>';
echo $cat::MARS.'<hr>';
echo $cat->food.'<br>'; // 无该属性
echo $cat->name.'<br>'; // 父类私有属性子类不可见,访问不到
echo $cat->type.'<br>';
echo $cat->nickName.'<br>';
echo $cat->feed.'<hr>';
echo $cat->run().'<hr>';
echo 'static<hr>';
echo $cat::$firstName.'<br>';
// echo $cat::$lastName.'<br>'; // 无法访问该静态属性
echo $cat::sleep().'<hr>';

运行结果

TIM截图20180905231602.png

总结

    通过这次课程,个人认为的重点是类的继承与方法重写,以及类中静态成员的声明与访问。

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