PHP 起步篇 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

域运算符

  在PHP中“::”这个叫范围解析操作符,又名域运算符。“::”符号可以认为是与C语言中的“.”相似的,而它更像C++中(Perl)的::类范围操作符。

  在没有声明任何实例的情况下访问类中的函数或者基类中的函数和变量很有用处。而:: 运算符即用于此情况。

<?php
class A {
  function example() {
      echo "I am the original function A::example().<br />\n";
  }
}

class B extends A {
  function example() {
    echo "I am the redefined function B::example().<br />\n";
    A::example();
  }
}

// A 类没有对象,这将输出
// I am the original function A::example().<br />
A::example();

// 建立一个 B 类的对象
$b = new B;

// 这将输出
//  I am the redefined function B::example().<br />
//  I am the original function A::example().<br />
$b->example();
?>