gpt4 book ai didi

php:确定从哪里调用函数

转载 作者:IT老高 更新时间:2023-10-28 11:53:31 24 4
gpt4 key购买 nike

有没有办法找出 PHP 中的函数是从哪里调用的?示例:

function epic()
{
fail();
}

function fail()
{
//at this point, how do i know, that epic() has called this function?
}

最佳答案

您可以使用 debug_backtrace() .

示例:

<?php

function epic( $a, $b )
{
fail( $a . ' ' . $b );
}

function fail( $string )
{
$backtrace = debug_backtrace();

print_r( $backtrace );
}

epic( 'Hello', 'World' );

输出:

Array
(
[0] => Array
(
[file] => /Users/romac/Desktop/test.php
[line] => 5
[function] => fail
[args] => Array
(
[0] => Hello World
)

)

[1] => Array
(
[file] => /Users/romac/Desktop/test.php
[line] => 15
[function] => epic
[args] => Array
(
[0] => Hello
[1] => World
)

)

)

关于php:确定从哪里调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2960805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com