Blogger Information
Blog 6
fans 0
comment 0
visits 3738
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类重载及实例-2019-10-08
゛\ぢ尛琦的博客
Original
481 people have browsed it

1. 实例演示四个属性重载的魔术方法的使用方式

实例

<?php
    namespace _1008;
    // 属性重载
// __get(), __set(), __isset(), __unset()
    class monkey{
        private $name;
        private $food;
        protected $achieve ='96';
        public function __construct($name,$food)
        {
            $this->name = $name;
            $this->food = $food;
        }
        // __get()

        public  function __get($name)
        {
            if(property_exists($this,$name)){
                return $this -> $name;
            }else{
                return '没有找到相应数据!';
            }
        }
        //__set()
        public function __set($name, $value)
        {
            if(property_exists($this,$name)){
                return $this->$name =$value;
            }else{
                return '没有找到相应数据!';
            }

        }
        //isset()
        public function __isset($name)
        {
            return isset($this->$name);
        }
        //unset()
        public function __unset($name)
        {
            unset($this->name);
        }

    }

    $obj = new monkey('孙悟空','香蕉');
    echo '__get()'.'<br>';
    echo '小猴的名字是:'.$obj->name.',他喜欢的食物是:'.$obj->food;
    echo '<hr>';
    echo '__set()'.'<br>';
    $obj ->name ='六耳猕猴';
    $obj ->food='蟠桃';
    echo '小猴的名字是:'.$obj->name.',他喜欢的食物是:'.$obj->food;
    echo '<hr>';
    echo '__isset()'.'<br>';
    var_dump(isset($obj->name));
    echo '<br>';
    var_dump(isset($obj->foods));


?>

运行实例 »

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

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

实例

<?php

namespace _1008;
function pro($a,$b){
    echo "{$a}*{$b}=".$a*$b;
}
echo pro(2,5).'<br>';
echo call_user_func(__NAMESPACE__.'\pro',4,9);
echo '<hr>';
echo call_user_func_array(__NAMESPACE__.'\pro',[9,8]);
echo '<hr>';
class demo2{
    public function pro($a,$b){
        return "{$a}*{$b}=".$a * $b;
    }
}
$obj = new demo2();
echo call_user_func_array([$obj,'pro'],[5,8]);
echo '<hr>';
class demo02{
    public static function pro($a,$b){
        return "{$a}*{$b}=".$a * $b;
    }
}
echo call_user_func_array([new demo02(),'pro'],[9,9]);



?>

运行实例 »

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

3. 实例演示方法重载的实现原理与参数获取方式

实例

<?php
namespace _1008;
class demo3{
    public function __call($name, $arguments)
    {
        return $name. '<br>数据列表: <pre>'.print_r($arguments,true);
    }
    public static function __callStatic($name, $arguments)
    {
        return $name. '<br>数据列表: <pre>'.print_r($arguments,true);
    }

}
$obj = new demo3();
echo $obj->one(1,2,3,4,5,6);
echo '<hr>';
echo demo3::two('一','二','三','四','五');

?>

运行实例 »

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

4. 实例演示数据库链接调用的实现原理与过程(静态方法重载__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($tableName)
    {
        $this->table = $tableName;
        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)
    {
        $this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
        return $this;
    }
    public function select()
    {
        $sql = 'SELECT '
            . $this->field
            . ' FROM '
            . $this->table
            . $this->where
            . $this->limit;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

}
class db{
    protected static $pdo =null;
    public static function connection(){
        self::$pdo =new \PDO('mysql:host=www.phpxuexi.com;dbname=php', 'root', 'root');
    }
    public static function __callStatic($name, $arguments)
    {
        self::connection();
        $query = new query(self::$pdo);
        return call_user_func_array([$query,$name],$arguments);
    }
}
$mobile = db::table('mobile')
    ->field('id,name')
    ->where('price > 3000')
    ->select();

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


?>

运行实例 »

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

Correction status:qualified

Teacher's comments:property_exists()这个方法用得好
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