大网数据广告活动
PHP 语言与运行手册

uopz_set_return · Provide a return value for an existing function

· 52CE 我爱测

uopz_set_return — Provide a return value for an existing function

uopz_set_return

(PECL uopz 5, PECL uopz 6, PECL uopz 7)

uopz_set_return — Provide a return value for an existing function

说明

function uopz_set_return(string $function, mixed $value, bool $execute = false): bool
function uopz_set_return(
    string $class,
    string $function,
    mixed $value,
    bool $execute = false
): bool

Sets the return value of the function to value. If value is a Closure and execute is set, the Closure will be executed in place of the original function. It is possible to call the original function from the Closure.

注意:

This function replaces uopz_rename().

参数

class

The name of the class containing the function

function

The name of an existing function

value

The value the function should return. If a Closure is provided and the execute flag is set, the Closure will be executed in place of the original function.

execute

If true, and a Closure was provided as the value, the Closure will be executed in place of the original function.

返回值

True if succeeded, false otherwise.

示例

示例 #1 uopz_set_return() example

<?php
uopz_set_return("strlen", 42);
echo strlen("Banana");
?>

以上示例会输出:

42

示例 #2 uopz_set_return() example

<?php
uopz_set_return("strlen", function($str) { return strlen($str) * 2; }, true );
echo strlen("Banana");
?>

以上示例会输出:

12

示例 #3 uopz_set_return() class example

<?php
class My {
    public static function strlen($arg) {
        return strlen($arg);
    }
}
uopz_set_return(My::class, "strlen", function($str) { return strlen($str) * 2; }, true );
echo My::strlen("Banana");
?>

以上示例会输出:

12

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。