PHP trait prototype, iterator usage

One, trait

1.1 trait (prototype)

In order to reduce the single-inheritance language single restriction, traits can reuse single-class method sets in independent single-classes in different hierarchical structures.
example:

<?php
  // 定义原型
  trait A {
    
    
    public function getInfo() {
    
    
      echo 'getInfo function';
    }
  }

  // 使用原型
  class Student {
    
    
    use A; // 代码复用
  }

  // 测试
  $stu=new Student;
  $stu->getInfo();
?>

effect:

Insert picture description here


1.2 Introduce multiple traits

Example:

<?php
  // 定义原型
  trait A {
    
    
    public function getInfo() {
    
    
      echo 'getInfo function<br>';
    }
  }

  // 定义原型
  trait B {
    
    
    public function getInfo2() {
    
    
      echo 'getInfo1 function<br>';
    }
  }
  // 使用原型
  class Student {
    
    
    use A,B; // 代码复用
  }

  // 测试
  $stu=new Student;
  $stu->getInfo();
  $stu->getInfo2();
?>

effect:
Insert picture description here

1.3 Combining traits and inheritance

Example:

<?php
  // 定义原型
  trait A {
    
    
    public function getInfo() {
    
    
      echo '这是trait原型<br>';
    }
  }

  class Person {
    
    
    public function getInfo() {
    
    
      echo '这是person类<br>';
    }
  }

  // 继承类同时代码复用
  class Student extends Person {
    
    
    use A; // 继承了getInfo,又被A中getInfo覆盖
  }

  // 测试
  $stu=new Student;
  $stu->getInfo();
?>

1.4 Resolve conflicts of the same name

Example:

<?php
  // 定义原型
  trait A {
    
    
    public function getInfo() {
    
    
      echo '这是trait原型A<br>';
    }
  }
  trait B {
    
    
    public function getInfo() {
    
    
      echo '这是trait原型B<br>';
    }
  }
  
  // 使用原型
  class Student {
    
    
    use A,B {
    
     // 引入a,b的trait,同时解决名称冲突
        // 方法一:方法替换
        A::getInfo insteadof B; // 将a中的getInfo替换掉b中掉getInfo
        // 方法二:改名
        B::getInfo as show;
        // 
    }
  }
  // 测试
  $stu=new Student;
  $stu->getInfo();
  $stu->show();
?>

effect:
Insert picture description here

1.5 Change permissions

Example:

<?php
  trait A {
    
    
    private function show() {
    
    
      echo 'show<br>';
    }
  }

  class Student {
    
    
    use A {
    
    
      show as public; // 将show方法权限设为public
      show as public show2; // 将show方法权限设为public并改名show2
    }
  }
  $stu=new Student;
  $stu->show();
  $stu->show2();

?>

effect:
Insert picture description here

Two, iterator

2.1 Traverse the array

Manually traverse the array
Steps:
1. Reset the array pointer reset()
2. Check whether the pointer is legal
3. Get the current value current()
4. Get the current key key()
5. Move the pointer down next()
Code implementation:

<?php
  $stu=['tom', 'jack', 'jackson', 'jane'];
  reset($stu); // 复位指针
  while(key($stu) !== null) {
    
     //  键合法 
    echo key($stu), '-', current($stu), '<br>';
    next($stu); // 指针下移
  }
?>

effect:
Insert picture description here

2.2 Iterator

Iterator is a built-in interface of php.
Scenario: traverse the object and get the number of groups saved by the attributes in the object

<?php
  // 定义类实现迭代器接口
  class MyClass implements Iterator {
    
    
    private $list=array();
    public function addStu($name) {
    
    
      $this->list[]=$name;
    }
    // 实现接口中的复位方法
    public function rewind () {
    
    
      reset($this->list);
    }
    // 验证当前指针是否合法
    public function valid() {
    
    
      return key($this->list) !== null;
    }
    // 获取值
    public function current() {
    
    
      return current($this->list);
    }
    // 获取键
    public function key() {
    
    
      return key($this->list);
    }
    // 指针下移
    public function next() {
    
    
      next($this->list);
    }
  }

  // 创建班级
  $class=new MyClass();
  // 添加学生
  $class->addStu('tom');
  $class->addStu('berry');
  $class->addStu('ketty');
  // 遍历班级
  foreach($class as $k=>$v) {
    
    
    echo "{
      
      $k}-{
      
      $v}<br>";
  } 

?>

effect:
Insert picture description here

Three, PSR coding standard

3.1 Overview

1. PSR is short for PHP Standard Recomations. The PHP specification developed by the PHP FIG organization is the practice standard for PHP development.
2. At present, 6 sets of standards have been voted and passed, and have been supported and recognized by most PHP frameworks.
3. Website:

http://psr.phphub.org/

On the way of learning php, if you think this article is helpful to you, please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/112556538