register_tick_function

register_tick_function(callable $callback, mixed ...$args): bool

在 declare(ticks=N) 作用域内每 N 条底层语句触发一次回调——PHP 独有的逐语句钩子,其他主流语言均无对应物(本条目为各语言手写插桩对照)。

C C23

C23
#include <stdio.h>

/* PHP 在 declare(ticks) 下每条语句触发回调;C 最诚实的近似是
   编译期宏插桩计数 */
static int tick_count = 0;
#define STEP (tick_count++)

int main(void) {
    int a = STEP;
    int b = STEP;
    int c = STEP;
    printf("statements instrumented: %d\n", 3);
    printf("macro fired per statement: %s\n", tick_count == 3 ? "true" : "false");
    printf("C has no tick hook - the macro is manual instrumentation\n");
    return 0;
}
输出
statements instrumented: 3
macro fired per statement: true
C has no tick hook - the macro is manual instrumentation

C++ C++26

C++26
#include <iostream>

// PHP 每条语句触发回调;C++ 没有这种钩子——计数包装器是诚实的近似
static int tick_count = 0;

static void step(void (*f)()) {
    tick_count++;
    f();
}

int main() {
    step([] {});
    step([] {});
    step([] {});
    std::cout << "callback fired per call: " << (tick_count == 3 ? "true" : "false") << "\n";
    std::cout << "C++ has no tick hook - this is manual instrumentation\n";
    return 0;
}
输出
callback fired per call: true
C++ has no tick hook - this is manual instrumentation

C# v11.0.100

v11.0.100
// PHP 有逐语句 tick 钩子;C# 没有(Profiler API 属工具级)——
// 手动插桩是诚实的近似
int count = 0;
void Step(Action f) { count++; f(); }
Step(() => { });
Step(() => { });
Step(() => { });
Console.WriteLine("callback fired per call: " + (count == 3));
Console.WriteLine("C# has no tick hook - this is manual instrumentation");
输出
callback fired per call: True
C# has no tick hook - this is manual instrumentation

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// PHP 有逐语句 tick 钩子;Go 没有——计数包装器的手动插桩是诚实的近似
	tickCount := 0
	step := func(fn func()) {
		tickCount++
		fn()
	}
	step(func() {})
	step(func() {})
	step(func() {})
	fmt.Println("callback fired per call:", tickCount == 3)
	fmt.Println("Go has no tick hook - this is manual instrumentation")
}
输出
callback fired per call: true
Go has no tick hook - this is manual instrumentation

Java v27.0.1

v27.0.1
public class Main {
    static int count = 0;

    static void step(Runnable f) {
        count++;
        f.run();
    }

    public static void main(String[] args) {
        // PHP 有逐语句 tick 钩子;Java 没有(java.lang.instrument 属 agent 级工具)——
        // 手动插桩是诚实的近似
        step(() -> {});
        step(() -> {});
        step(() -> {});
        System.out.println("callback fired per call: " + (count == 3));
        System.out.println("Java has no per-statement hook - this is manual instrumentation");
    }
}
输出
callback fired per call: true
Java has no per-statement hook - this is manual instrumentation

JavaScript v26.13.1

v26.13.1
// PHP 有逐语句 tick 钩子;JS 没有——计数包装器的手动插桩是诚实的近似
let count = 0;
const step = (fn) => {
  count++;
  fn();
};
step(() => {});
step(() => {});
step(() => {});
console.log("callback fired per call:", count === 3);
console.log("JS has no tick hook - this is manual instrumentation");
输出
callback fired per call: true
JS has no tick hook - this is manual instrumentation

PHP v8.6.0

v8.6.0
// register_tick_function:在 declare(ticks=N) 作用域内每 N 条语句触发回调
// PHP 独有钩子:没有其他主流语言逐语句触发回调
declare(ticks=1);
$count = 0;
register_tick_function(function () use (&$count) { $count++; });
// 下面的每条简单语句都会推进 tick 计数器(ticks=1)
$a = 1;
$b = 2;
$c = 3;
// 确切计数取决于 opcode 结构,而不是仅语句数
printf("3 statements; callback fired %d times\n", $count);
var_dump($count >= 1);
输出
3 statements; callback fired 1 times
bool(true)

Python v3.15.2

v3.15.2
import sys

# PHP 在 declare(ticks) 下每条语句触发回调;Python 最接近的钩子是
# sys.settrace——仅调试用的逐行追踪器(只对新 frame 生效)
count = 0


def tracer(frame, event, arg):
    global count
    if event == "line":
        count += 1
    return tracer


def work():
    a = 1
    b = 2
    c = 3


sys.settrace(tracer)
work()  # 新 frame——settrace 钩子在其中的每行触发
sys.settrace(None)
print("line events fired (>= 3 statements):", count >= 3)
print("Python has no tick hook - settrace is debugging-only tooling")
输出
line events fired (>= 3 statements): True
Python has no tick hook - settrace is debugging-only tooling

Rust v1.99.1

v1.99.1
fn main() {
    // PHP 有逐语句 tick 钩子;Rust 没有——计数闭包的手动插桩是诚实的近似
    let mut count = 0;
    let mut step = |f: fn()| {
        count += 1;
        f();
    };
    step(|| {});
    step(|| {});
    step(|| {});
    println!("callback fired per call: {}", count == 3);
    println!("Rust has no tick hook - this is manual instrumentation");
}
输出
callback fired per call: true
Rust has no tick hook - this is manual instrumentation

TypeScript v26.13.1

v26.13.1
// PHP 有逐语句 tick 钩子;TS 没有——计数包装器的手动插桩是诚实的近似
let count = 0;
const step = (fn: () => void): void => {
  count++;
  fn();
};
step(() => {});
step(() => {});
step(() => {});
console.log("callback fired per call:", count === 3);
console.log("TS has no tick hook - this is manual instrumentation");
输出
callback fired per call: true
TS has no tick hook - this is manual instrumentation

语言差异说明

C:无任何逐语句钩子——最诚实的近似是编译期宏插桩(#define STEP (count++),示例);运行期真钩子只有调试器。

C++:同 C 无钩子——计数包装函数(示例)或 gcc/clang 的 -finstrument-functions(函数边界,非语句级)。

C#:无逐语句钩子——Profiler/IL 插桩属工具级(CLI Profiler API);手动包装器近似(示例)。

Go:无逐语句钩子——runtime 追踪(trace)是性能工具非回调钩子;手动包装器近似(示例)。

Java:无逐语句钩子——java.lang.instrument 是 agent 级字节码工具;手动包装器近似(示例)。

JavaScript:无逐语句钩子——Node --cpu-prof/调试器是工具级;手动包装器近似(示例)。

PHP:declare(ticks=N) + register_tick_function 是独有机制——每 N 条底层 tickable 语句触发回调(触发粒度按 zend opcode 结构计,不等于源码语句数,跨版本可能变化,示例如实记录本机计数); unregister_tick_function 注销;8.5 起回调在请求关闭阶段更晚停用(先于输出的清理完成后);现代替代思路:性能分析用 Xdebug/opesnoop 类工具,不要在生产用 tick。

Python:sys.settrace 逐行事件追踪是最接近的钩子——但官方定位是调试器/覆盖率工具(PEP 669 monitoring 是新后端);只对新进入的 frame 生效(模块顶层不触发,示例把语句放进 work());性能开销大,非生产钩子。

Rust:无逐语句钩子——debug_assert!/宏是编译期;手动包装器近似(示例)。

TypeScript:同 JavaScript——无逐语句钩子。