Correction status:Uncorrected
Teacher's comments:
static:: 在静态集成的上下文环境中,动态的设置方法调用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <?php class A{ public static function who(){ echo __CLASS__; } public static function test(){ //self::who(); static::who(); //静态方法中动态调用 } } class B extends A{ public static function who(){ echo __CLASS__; } } //静态继承的上下文之间 B::test(); echo '<br>'; //父类 class Connection{ public static function connect(){ //return static :: config(); 子类继承后调用的会变成子类中的连接参数 return self::config(); //子类继承后调用的仍然是父类中的连接参数 } public static function config(){ return new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); } } //创建连接子类 class Link extends Connection{ public static function config(){ //错误的连接密码 return new PDO('mysql:host=127.0.0.1;dbname=php','root',''); } } $pdo = Link::connect(); $staffs = $pdo->query('select * from `staff` limit 5'); foreach($staffs as $staff){ print_r($staff); echo '<br>'; } ?> </body> </html>
点击 "运行实例" 按钮查看在线实例