Home  >  Article  >  Backend Development  >  Difference between class static call and scope resolution operator in PHP

Difference between class static call and scope resolution operator in PHP

jacklove
jackloveOriginal
2018-07-03 17:42:272132browse

This article mainly introduces the difference between class static calling and range resolution operators in PHP. Friends in need can refer to it

The specific code is as follows:

<?php
//在子类或类内部用“::”调用本类或父类时,不是静态调用方法,而是范围解析操作符。
class ParentClass {
 public static $my_static = &#39;parent var &#39;;
 function test() {
  self::who(); // 输出 &#39;parent&#39; 是范围解析,不是静态调用
  $this->who(); // 输出 &#39;child&#39;
  static::who(); // 延迟静态绑定 是范围解析,不是静态调用
 }
 function who() {
  echo &#39;parent<br>&#39;;
 }
}
class ChildClass extends ParentClass {
 public static $my_static = &#39;child var &#39;;
 function who() {
  echo &#39;child<br>&#39;;
 }
}
$obj = new ChildClass();
$obj->test();
echo ChildClass::$my_static;//静态调用

The above output

parent

child

child

child var

Summary

The above is the editor’s introduction to the difference between class static calls and range resolution operators in PHP. I hope it will be helpful to you. If you have any questions If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the php Chinese website!

Articles you may be interested in:

Detailed examples of stack and queue functions implemented by PHP based on arrays

Detailed explanation of error handling and exception handling methods based on PHP7

Explanation of predefined variables for PHP learning

The above is the detailed content of Difference between class static call and scope resolution operator in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn