- PHP 教程
- PHP - 首页
- PHP - 简介
- PHP - 安装
- PHP - 历史
- PHP - 特性
- PHP - 语法
- PHP - Hello World
- PHP - 注释
- PHP - 变量
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 变量
- PHP - 常量
- PHP - 魔术常量
- PHP - 数据类型
- PHP - 类型转换
- PHP - 类型混杂
- PHP - 字符串
- PHP - 布尔值
- PHP - 整数
- PHP - 文件与I/O
- PHP - 数学函数
- PHP - Heredoc & Nowdoc
- PHP - 复合类型
- PHP - 文件包含
- PHP - 日期与时间
- PHP - 标量类型声明
- PHP - 返回类型声明
- PHP 运算符
- PHP - 运算符
- PHP - 算术运算符
- PHP - 比较运算符
- PHP - 逻辑运算符
- PHP - 赋值运算符
- PHP - 字符串运算符
- PHP - 数组运算符
- PHP - 条件运算符
- PHP - 展开运算符
- PHP - 空值合并运算符
- PHP - 比较运算符(太空船运算符)
- PHP 控制语句
- PHP - 决策
- PHP - If…Else 语句
- PHP - Switch 语句
- PHP - 循环类型
- PHP - For 循环
- PHP - Foreach 循环
- PHP - While 循环
- PHP - Do…While 循环
- PHP - Break 语句
- PHP - Continue 语句
- PHP 函数
- PHP - 函数
- PHP - 函数参数
- PHP - 按值传递
- PHP - 按引用传递
- PHP - 默认参数
- PHP - 具名参数
- PHP - 可变参数
- PHP - 返回值
- PHP - 传递函数
- PHP - 递归函数
- PHP - 类型提示
- PHP - 变量作用域
- PHP - 严格类型
- PHP - 匿名函数
- PHP - 箭头函数
- PHP - 可变函数
- PHP - 局部变量
- PHP - 全局变量
- PHP 超全局变量
- PHP - 超全局变量
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 文件处理
- PHP - 文件处理
- PHP - 打开文件
- PHP - 读取文件
- PHP - 写入文件
- PHP - 文件是否存在
- PHP - 下载文件
- PHP - 复制文件
- PHP - 追加文件
- PHP - 删除文件
- PHP - 处理CSV文件
- PHP - 文件权限
- PHP - 创建目录
- PHP - 列出文件
- 面向对象的PHP
- PHP - 面向对象编程
- PHP - 类和对象
- PHP - 构造函数和析构函数
- PHP - 访问修饰符
- PHP - 继承
- PHP - 类常量
- PHP - 抽象类
- PHP - 接口
- PHP - 特性
- PHP - 静态方法
- PHP - 静态属性
- PHP - 命名空间
- PHP - 对象迭代
- PHP - 封装
- PHP - final 关键字
- PHP - 重载
- PHP - 克隆对象
- PHP - 匿名类
- PHP Web 开发
- PHP - Web 概念
- PHP - 表单处理
- PHP - 表单验证
- PHP - 表单邮件/URL
- PHP - 完整表单
- PHP - 文件包含
- PHP - GET & POST
- PHP - 文件上传
- PHP - Cookie
- PHP - Session
- PHP - Session 选项
- PHP - 发送邮件
- PHP - 过滤输入
- PHP - Post-Redirect-Get (PRG)
- PHP - 闪存消息
- PHP 高级
- PHP - MySQL
- PHP.INI 文件配置
- PHP - 数组解构
- PHP - 编码规范
- PHP - 正则表达式
- PHP - 错误处理
- PHP - Try…Catch
- PHP - 调试Bug
- PHP - 针对C语言开发人员
- PHP - 针对Perl语言开发人员
- PHP - 框架
- PHP - Core PHP vs. 框架
- PHP - 设计模式
- PHP - 过滤器
- PHP - JSON
- PHP - 异常
- PHP - 特殊类型
- PHP - 哈希
- PHP - 加密
- PHP - is_null() 函数
- PHP - 系统调用
- PHP - HTTP 认证
- PHP - 交换变量
- PHP - Closure::call()
- PHP - 过滤后的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 预期
- PHP - use 语句
- PHP - 整数除法
- PHP - 已弃用的特性
- PHP - 已移除的扩展和SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 进程
- PHP - PDO 扩展
- PHP - 内置函数
- PHP 有用资源
- PHP - 速查表
- PHP - 问答
- PHP - 快速指南
- PHP - 在线编译器
- PHP - 有用资源
- PHP - 讨论
PHP – 静态方法
PHP 中的“static”关键字用于在 PHP 类中定义静态属性和静态方法。需要注意的是,static 关键字也用于定义静态变量和静态匿名函数。本章讨论 PHP 类中的静态方法。
在类定义中,使用 static 修饰符声明的函数成为其静态方法。
class myclass {
public static function myStaticMethod() {
// ...
}
无需创建类的实例即可调用其静态方法。静态方法通过类名和作用域解析运算符来调用。静态方法调用的语法为:
myclass::myStaticMethod();
由于静态方法无需创建类的实例即可调用,因此在静态方法内部无法使用伪变量 $this。允许通过对象调用静态方法,尽管将实例方法作为静态方法调用会引发错误。
示例
请看下面的例子:
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
}
}
myclass::mystaticmethod();
$obj = new myclass;
$obj->myinstancemethod();
$obj->mystaticmethod();
myclass::myinstancemethod();
?>
它将产生以下输出:
This is a static method This is an instance method This is a static method PHP Fatal error: Uncaught Error: Non-static method myclass::myinstancemethod() cannot be called statically
静态方法中的“self”关键字
如果需要在同一类中定义的实例方法内部调用静态方法,则必须使用 self 关键字引用类名,后跟作用域解析运算符(例如 self::mystaticmethod)
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
echo "calling static method from instance method" . PHP_EOL;
self::mystaticmethod();
}
}
$obj = new myclass;
$obj->myinstancemethod();
?>
它将产生以下输出:
This is an instance method calling static method from instance method This is a static method
使用“parent”关键字
在继承的情况下,可以通过对象(派生类对象)或通过派生类的实例方法内部调用基类中定义的静态方法,方法是使用“parent”关键字引用它。
示例
请看下面的例子:
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
echo "calling static method from instance method" . PHP_EOL;
self::mystaticmethod();
}
}
class mynewclass extends myclass {
public function myfunction() {
echo "This an instance method of the derived class" . PHP_EOL;
echo "Calling static method of the parent class" . PHP_EOL;
parent::mystaticmethod();
}
}
$obj = new mynewclass;
mynewclass::mystaticmethod();
$obj->myfunction();
?>
它将产生以下输出:
This is a static method This an instance method of the derived class Calling static method of the parent class This is a static method
另一个类中的静态方法
完全可以从一个类中调用另一个类中的静态方法。必须使用其类名前缀加上作用域解析运算符限定其名称。
示例
请看下面的例子:
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
}
#this is not a derived class
class mynewclass {
public function myfunction() {
echo "This an instance method" . PHP_EOL;
echo "Calling static method of the another class" . PHP_EOL;
myclass::mystaticmethod();
}
}
$obj = new mynewclass;
$obj->myfunction();
?>
它将产生以下输出:
This an instance method Calling static method of another class This is a static method
由于$this伪变量在静态方法中不可用,因此无法在静态方法内部访问对象的实例变量。它只能处理类的静态属性。
示例
请看下面的例子:
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct() {
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
public static function mystaticmethod() {
echo "Number of objects available: " . self::$var1 . PHP_EOL;
}
}
for ($i=1; $i<=3; $i++) {
$obj = new myclass;
}
myclass::mystaticmethod();
?>
它将产生以下输出:
object number 1 object number 2 object number 3 Number of objects available: 3
广告