批改状态:合格
老师批语:可能现在还不能写出自己的代码, 只能照抄案例了, 能理解了也行吧
1、实例演示四个属性重载的魔术方法的使用方式
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/10/8
* Time: 23:17
*/
/*
*10月8日作业
1. 实例演示四个属性重载的魔术方法的使用方式
2. 实例演示call_user_func_array()回调执行函数/对象/类的方法的流程与方式
3. 实例演示方法重载的实现原理与参数获取方式
4. 实例演示数据库链接调用的实现原理与过程(静态方法重载__callStatic实现)
*/
class Test
{
private $name = 'test';
function __get($name)
{
return $this->$name;
}
function __set($name, $value)
{
// TODO: Implement __set() method.
return $this->$name = $value;
}
function __isset($name)
{
// TODO: Implement __isset() method.
return isset($this->$name);
}
function __unset($name)
{
// TODO: Implement __unset() method.
unset($this->$name);
}
}
$obj = new Test();
echo $obj->name;
echo '<hr>';
$obj->name = '设置新值';
echo $obj->name;
echo '<hr>';
echo isset($obj->name) ? '存在' : '不存在';
echo '<hr>';
unset($obj->name);
echo @preg_match("/Undefined property/i",$obj->name) ? '存在' : "被注销";点击 "运行实例" 按钮查看在线实例
2、实例演示call_user_func_array()回调执行函数/对象/类的方法的流程与方式
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/10/8
* Time: 23:50
*/
namespace _1008;
function sum($a,$b){
return "{$a} + {$b} = " . ($a + $b);
}
echo "<p style='color: red'>1、回调执行函数的方式</p>";
echo call_user_func_array('\_1008\sum',[13,15]);
class Test
{
function sum($a,$b){
return "{$a} + {$b} = " . ($a + $b);
}
static function sumStatic($a,$b){
return "{$a} + {$b} = " . ($a + $b);
}
}
$obj = new Test();
echo "<p style='color: red'>2、回调执行对象\类的方法的方式</p>";
echo '(1)$obj 的方式<br>';
echo call_user_func_array([$obj,'sum'],[11,33]);
echo '<br>';
echo '(2)new 类的方式<br>';
echo call_user_func_array([new Test(),'sum'],[11,33]);
echo '<br>';
echo '(3)static 静态方法直接Test::class,类调用的方式<br>';
echo call_user_func_array([Test::class,'sumStatic'],[22,33]);点击 "运行实例" 按钮查看在线实例
3、实例演示方法重载的实现原理与参数获取方式
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/10/9
* Time: 0:14
*/
class Test
{
function __call($name, $arguments)
{
// TODO: Implement __call() method.
return '方法为:'. $name . '<br>' . '参数为:<pre>' . print_r($arguments,true);
}
static function __callStatic($name, $arguments)
{
// TODO: Implement __callStatic() method.
return '静态方法为:'. $name . '<br>' . '参数为:<pre>' . print_r($arguments,true);
}
}
$obj = new Test();
echo $obj->getInfo(1,2,3);
echo '<br>';
echo Test::getInfo1(4,5,6);点击 "运行实例" 按钮查看在线实例
4、实例演示数据库链接调用的实现原理与过程(静态方法重载__callStatic实现)
<?php
// namespace _query;
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($field = '*'){
$this->field = empty($field) ? '*' : $field;
return $this;
}
public function where($where = ''){
$this->where = empty($where) ? '' : ' WHERE ' . $where;
return $this;
}
public function limit($limit = ''){
$this->limit = empty($limit) ? '' : ' LIMIT ' .$limit;
return $this;
}
public function select(){
$smtp = $this->pdo->prepare("select " . $this->field . " from " . $this->table . $this->where . $this->limit);
$smtp->execute();
// die($smtp->debugDumpParams());
return $smtp->fetchAll(\PDO::FETCH_ASSOC);
}
}
class Db
{
public static $pdo = null;
public static function connect(){
self::$pdo = new \PDO("mysql:host=127.0.0.1;dbname=test",'root','000000');
}
public static function __callStatic($name,$args){
self::connect();
$query = new Query(self::$pdo);
return call_user_func_array([$query,$name],$args);
}
}
$staffs = Db::table('testinfo')
->field('id,name')
->where('id>22')
->limit(31)
->select();
foreach ($staffs as $staff) {
print_r($staff); echo '<br>';
}点击 "运行实例" 按钮查看在线实例
总结:
1、属性重载魔术方法__get\__set\__isset\__unset; 分别实现对不存在或不能访问的属性读取、赋值、判断是否存在、注销等操作
2、call_user_func_array() 该函数有2个参数,第一个为函数字符串,第二个为参数数组。在类中第一个参数也为数组模式传值,第一个为当前对象或者类,第二个为函数字符串
3、__call、__callStatic 分别实现对不存在、无法调用的方法、静态方法调用。分别存在2个参数,第一个均为调用方法,第二个为参数数组
4、数据库链式调用,分别在定义的方法直接返回当前对象利用对象继续调用定义的方法。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号