Blogger Information
Blog 30
fans 1
comment 0
visits 24043
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类成员重载以及实战 20191008
阿乎乎的学习
Original
529 people have browsed it

第一个知识点  :: 范围解析符
类里创建静态资源  static ;访问静态资源,类外使用类名加范围解析符,demo::  类里使用self::进行访问,也可以使用类名,但比较麻烦。
第二个知识点  常量 和 类常量。常量只能定义和使用,不可更新,不可以删除,没有作用域限制。
常量用define('常量名','常量值')来定义,类常量用const 常量名=常量值。 define允许常量值是一个表达式,const只允许常量值是一个自变量。define不能使用在类里面。
第三个知识点 对象属性重载 当在类外访问一个不存在或者没有权限访问的类属性的时候就需要进行属性重载。读操作function __get($name) ,写操作function __set($name,$value),检测function __isset($name),销毁function __unset($name)。
第四个知识点 回调函数  call_user_func(要执行的回调函数,参数1,参数2...)  call_user_func_array(要执行的函数,参数1,参数2...)。
第五个知识点 方法重载 __call($name,$args)访问不存在的方法或者无权限访问的方法的时候自动调用。__callStatic($name,$args)访问不存在的方法或者无权限访问的方法的时候自动调用。

作业:实例演示四个属性重载的魔术方法的使用方式

实例

<?php
namespace __1008;
class  demo {
    private $username;
    protected $age;
    private $sex;
    private $hobby;
    public function __construct($username,$age,$sex,$hobby)
    {
        $this->username=$username;
        $this->age=$age;
        $this->sex=$sex;
        $this->hobby=$hobby;
    }
    public function __set($name,$value)//更新
    {
//        $this->$name=$value;
        if($this->$name===$this->age){
            return ($this->username==='admin') ? $this->$name=$value : '无权修改';
        }
    }
    public function __get($name){     //访问
        if($this->$name===$this->age){   //如果访问的是年龄
            return ($this->username==='admin') ? $this->$name : '无权查看';  //用户名为admin可以查看年龄,其他用户返回无权查看
        }
        return $this->$name;
    }
    public function __isset($name)  //检测
    {
        return isset($this->$name);
    }
    public function __unset($name)  //删除
    {
        unset($this->$name);
    }
}
$obj=new demo('小白',18,'男','上网');
echo $obj->username;
echo '<br>';
echo $obj->age;
echo '<br>';
echo isset($obj->hobby) ? '存在' :'不存在';
echo '<hr>';
$obj1=new demo('admin',18,'男','上网');
echo $obj1->username;
$obj1->age=888;
echo '<br>';
echo $obj1->age;
unset($obj1->hobby);   //删除对象obj1里的爱好
echo '<br>';
echo isset($obj1->hobby) ? '存在' :'不存在';  //检测对象obj1里的爱好是否存在

运行实例 »

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

作业二:实例演示call_user_func_array()回调执行函数/对象/类的方法的流程与方式

实例

<?php
namespace __1008;
//以回调函数来调用函数

function sum($a,$b){
    return ($a+$b);
}
echo call_user_func(__NAMESPACE__.'\sum',5,2);
echo '<br>';
$list=[5,6];
echo call_user_func_array(__NAMESPACE__.'\sum',$list);
echo '<hr>';
//以回调函数来调用类属性,方法
class demo3{
    public function  sum1($a,$b){
        return ($a+$b);
    }
    public static function  sum2($a,$b){
        return ($a+$b);
    }
}
$obj=new demo3();
echo call_user_func_array([$obj,'sum1'],[5,55]);
echo '<hr>';
//echo call_user_func_array(__NAMESPACE__.'\demo3::sum1',[7,15]);
echo call_user_func_array([demo3::class,'sum2'],[5,3]);

运行实例 »

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

作业三:实例演示方法重载的实现原理与参数获取方式

实例

<?php
namespace __1008;
class demo4{
    //方法重载
    public function __call($name,$args)
    {
        return '调用的方法<span style="color:red">'.$name.'</span>不存在,'.'参数列表<pre>'.print_r($args,true).'</pre>';
    }
    //静态方法重载
    public static function __callStatic($name, $args)
    {
        return '调用的方法<span style="color:red">'.$name.'</span>不存在,'.'参数列表<pre>'.print_r($args,true).'</pre>';
    }
}
//方法调用
$obj1=new demo4();
echo $obj1->arg(1,2);
//静态方法调用
$obj2=demo4::agr(5,6);
echo $obj2;

运行实例 »

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

作业:实例演示数据库链接调用的实现原理与过程(静态方法重载__callStatic实现)

实例

<?php
namespace  __1008;
class query{
    //连接对象
    public $pdo=null;
    //表名
    public $table;
    //查询字段
    public $field='*';
    //查询条件
    public $where;
    //查询数量
    public $limit;
    //创建构造方法用来连接数据库
    public function __construct($pdo)
    {
        $this->pdo=$pdo;
    }
    //设置连接表名
    public function table($table){
        $this->table=$table;
        return $this;
    }
    //设置字段名
    public function field($fields='*'){
        //如果字段名为空返回默认值*查询全部,不为空则返回字段名
        $this->field =empty($fields) ? '*' : $fields;
        return $this;
    }
    //设置查询条件
    public function where($where=''){
        //如果查询条件为空返回默认值空,不为空则返回查询条件
        $this->where=empty($where) ? $where : 'WHERE'.$where;
        return $this;
    }
    //设置显示数量
    public function limit($limit)
    {
        //如果显示数量为空,则返回默认显示所有,不为空返回limit值
        $this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
        return $this;
    }
    //SQL语句拼接
    public function select(){
        //拼接SQL语句  SELECT * FROM tableName WHRER 条件 LIMIT 数值
        $sql='SELECT '.$this->field.' FROM '.$this->table.$this->where.$this->limit;
        //pdo预处理sql语句
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();
        //die($stmt->debugDumpParams());  // 查看生成的sql
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}

class DB
{
    // 连接对象
    protected static $pdo = null;

    // 数据库的连接方法
    public static function connection()
    {
        self::$pdo = new \PDO('mysql:host=127.0.0.1;dbname=www.tao.io', 'root', 'root');
    }

    public static function __callStatic($name, $arguments)
    {
        // 连接数据库
        self::connection();
        // 实例化查询类,将连接对象做为参数
        $query = new Query(self::$pdo);
        // 调用查询对象$query中的对应的方法
        return call_user_func_array([$query, $name],$arguments);
    }
}
$user= DB::table('user')
    ->field()
    ->where()
    ->limit(5)
    ->select();

foreach ($user as $v) {
    print_r($v); echo '<br>';
}

运行实例 »

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

自己写写不出来,总是得看一下,看样子需要多练!


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!