Blogger Information
Blog 19
fans 0
comment 2
visits 18493
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
20180903类的声明与实例化、属性重载、类的继承与方法重写、静态成员
乂汁的blog
Original
1225 people have browsed it

一、概述

本节课讲述基础知识,类、实例、属性、方法、继承、重载、重写、静态成员几个关键字展开。

二、作业

1、类的声明与实例化

实例

<?php
class Girl{

}
$hdd = new Girl();
$hdd -> name = 'hdd';
$hdd -> xiongwei = '70';
$hdd -> tunwei = '70';
$hdd -> yaowei = '70';
$hdd -> dance = function (){
    return '极乐净土';
};
echo $hdd ->name.'的三维是'.'、'.$hdd->xiongwei.'、'.$hdd->yaowei.'、'.$hdd->tunwei;
echo '他会跳'.call_user_func($hdd->dance).'<hr>';

class Boy{
    public $name;
    public $salary = 6800;
    private $age = 20 ;//私有成员本类访问
    protected $sex = 1;//受保护成员仅限本类或者子类访问


public function getSex(){
    return ($this->sex == 1) ? '女':'男';
}
public function getAge(){
    return ($this->sex == 0) ? $this->age : '保密';
}
}

$hdd2 = new Boy();
var_dump(is_null($hdd2->name));
echo $hdd2->salary,'<hr>';
echo '胡蛋蛋是',$hdd2->getSex(),'年龄',$hdd2->getAge(),'<hr>';

运行实例 »

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

2、类常量与类属性的重载

实例

<?php
class Hdd{
    public $name = 'HDD';
    private $xiongwei = '70';
    private $tunwei = '70';
    private $yaowei = '70';
    private $type = '502';
    public function __construct($tunwei,$xiongwei,$yaowei){
        $this->tunwei = $tunwei;
        $this->xiongwei = $xiongwei;
        $this->yaowei = $yaowei;
        $this->show();
    }
    public function show(){
        echo $this->name.'的SW'.$this->xiongwei.$this->yaowei.$this->tunwei.'他属于'.$this->type;

    }
    //获取属性重载,返回属性名字。isset返回是布尔值
    public function __get($name)
    {
        if (isset($this->$name) && $name != 'xiongwei'){
            return $this->$name;
        }else{
            echo '无法得到';
        }



    }
    //更新属性重载
    public function __set($name, $value)
    {
        if ($name == 'xiongwei'){
            echo $name,'不可修改';
        }
        $this->$name = $value;
    }
    //检测属性重载
    public function __isset($name)
    {
        if ($name == 'type'){
            return false;
        }
        return isset($this->$name);

    }
    //销毁属性重载
    public function __unset($name)
    {
        if ($name = 'xiongwei' || $name = 'type')
        {
            return false;
        }
        unset($this->$name);
    }


}
$hdd3 = new Hdd('80','80','80');
echo '<hr>'.$hdd3->name.'<hr>';
$hdd3->type2;
$hdd3->type2 = 901;
echo isset($hdd3->type2)?'有':'无';
echo $hdd3->type2.'<hr>';
echo (isset($hdd3->type3)? '有':'无').'<hr>';
unset($hdd3->type2);
echo isset($hdd3->type2)?'有':'无';

运行实例 »

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

3、类的继承与方法重写

实例

<?php
class Demo{
    public $name;
    protected $age;
    private $salary;
    const ID_NAME = 'llllll';
    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

}
class Demo_1 extends Demo{
    private $sex;
    const ID_NAME = 'mmmmm';
    public function __construct($name, $age,$sex = 'male')
    {
        parent::__construct($name, $age);
        $this->sex = $sex;
    }
    public function __get($name){
        if (isset($this->$name)){
            return $this->$name;
        }
        return '非法属性';
    }
}
$demo1 = new Demo_1('hdd','9','famale');
echo $demo1->name.'<hr>';
echo $demo1->age.'<hr>';
echo $demo1->sex.'<hr>';
echo $demo1->salary.'<hr>';//未出现结果,
//因为$salary是受保护的父类对象。子类是无法继承的
echo Demo_1::ID_NAME.'<hr>';
echo $demo1->sex.'<hr>';

运行实例 »

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

4、静态成员声明与访问

实例

<?php
class Mysql{
    public static $pdo = null;
    protected static $db = [
        'type'=>'mysql',
        'host'=>'127.0.0.1',
        'dbname'=>'php',
        'user'=>'root',
        'pass'=>'root'
    ];
    public static function connect()
{
        $dsn = self::$db['type'].':host='.self::$db['host'].';dbname='.self::$db['dbname'];
        self::$pdo = new PDO($dsn,self::$db['user'],self::$db['pass']);
    }
    public static function select($table,$fields='*',$num=5){
        $stmt = self::$pdo->prepare(
            "SELECT {$fields} FROM {$table} LIMIT {$num};"
        );
        $stmt->execute();
        return $stmt->fetchALL(PDO::FETCH_ASSOC);
    }
}

Mysql::connect();
$res = Mysql::select('staff','name,age,salary',6);
echo '<pre>',var_export($res).'</pre>';

运行实例 »

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

运行截图:

1.png2.png3.png4.png

四、总结

1、受保护成员仅限本类或者子类访问
2、受保护的父类对象,子类是无法继承的

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