Blogger Information
Blog 49
fans 0
comment 4
visits 41875
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
后期静态绑定的使用场景与实例说明
过儿的博客
Original
767 people have browsed it

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>

运行实例 »

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

1.png

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