是否可以从扩展中调用 PHP_FUNCTION
让我对标题进行更详细的说明。例如考虑 PHP_FUNCTION(session_start)。我是否能够从另一个 PHP_FUNCTION 的 session_id 中调用 session_start (这只是为了说明而不是实际目的)?
let me elaborate more on the Title. Consider for example PHP_FUNCTION(session_start). Will I be able to invoke session_start from within session_id which is another PHP_FUNCTION (this is just for illustration not the actual purpose)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,是的,但你应该尽可能避免它。编写函数内部实现的主要好处之一是,与 PHP 中发生的情况相反,C 函数调用很便宜。此外,在 C 代码中内部调用 PHP 函数是相对痛苦的。
例如,在
session_start
的情况下,您有php_session_start
,它由会话扩展公开。根据我在第一段中所描述的内容,扩展通常会导出对其他人可能有用的 C 函数。事实上,内部 PHP 函数
foo
需要调用内部 PHP 函数bar
,如果可能的话,最好的策略是定义一个辅助函数(非PHP_FUNCTION
) C 函数大部分实现为bar
。然后PHP_FUNCTION(foo)
和PHP_FUNCTION(bar)
都可以调用该辅助函数。无论如何,调用 PHP 函数的最简单方法是使用
call_user_function
:变体
call_user_function_ex
还允许在参数应该通过引用发送时禁止分隔,并指定一个符号桌子。如果相关函数是内部函数 (
PHP_FUNCTION
) 或在用户空间中定义的,这都将起作用。如果它是常规函数,您应该使用 EG(function_table) 作为第一个参数,第二个参数应该为 NULL,我认为您可以找出其他参数。如果多次执行该函数,效率不是很高。在这种情况下,请参阅 "Zend_API.h" 中的函数从
zend_fcall_
开始。我不建议使用其他选项来调用内部函数,例如手动设置参数堆栈和其他技巧以及手动调用底层 C 函数。
Well, yes, but you should avoid it as much as possible. One of the main benefits of writing internal implementations of functions is that, contrary to what happens in PHP, C function calls are cheap. Additionally, calling PHP functions internally in C code is relatively painful.
For instance, in the case of
session_start
, you havephp_session_start
, which is exposed by the session extension. Owing to what I described in the first paragraph, extensions will usually export C functions that may be useful to others.In fact, the internal PHP function
foo
needed to call the internal PHP functionbar
, the best strategy, if possible, would be to define an auxiliary (nonPHP_FUNCTION
) C function with most of the implementation ofbar
. Then bothPHP_FUNCTION(foo)
andPHP_FUNCTION(bar)
could call that auxiliary function.Anyway, the easiest way to call PHP functions is to use
call_user_function
:The variant
call_user_function_ex
also allows prohibiting separation when the argument should be sent by reference by it's not and specifying a symbol table.This will work both if the relevant function is internal (
PHP_FUNCTION
) or was defined in userspace. If it's a regular function, you should useEG(function_table)
as the first argument, the second should be NULL and I think you can figure out the others.If you execute the function several times, this is not very efficient. In that case, see the functions in "Zend_API.h" that start with
zend_fcall_
.I wouldn't recommend other options to call internal functions, such as manually setting up the arguments stack and other trickery and them manually calling the underlying C function.
IE。
session_start(session_id())
是的,但是在这种情况下它没有意义,因为
session_id()
要求会话已经启动。ie.
session_start(session_id())
Yes, however in this case it doesn't make sense because
session_id()
requires the session to already be started.