如何在 PHP 中定义带有参数的宏,就像在 C++ 中一样?
目标是有这样的东西:
#define sum( f1, f2 ) ( f1 + f2 )
PHP 中的 4 在哪里
sum(2,2)
。这有可能吗?更好的是,如果我可以调用:
$s = 'first';
APPEND ' and second';
当追加将被定义为附加到 $s
的函数/方法/其他内容时,那么在这两行之后 $s
将是“第一和第二”。
Target is to have something like:
#define sum( f1, f2 ) ( f1 + f2 )
where
sum(2,2)
is 4 in PHP. Is that somehow possible? Even better would be if I could call:
$s = 'first';
APPEND ' and second';
when append will be defined as function/method/something else which is appending to $s
so after those 2 lines $s
would be 'first and second'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C 中宏的要点是它们在编译时扩展。
因此,使用宏不会影响代码的速度,而执行相同操作的函数则会影响代码的速度。
因此,一个使用的例子是:
上面的代码将被编译器直接翻译成:
其要点在于它比定义一个函数要快,比如这个:
因为没有开销(在函数中,您需要将参数推入堆栈等)。
在像 PHP 这样的解释性语言中,上述情况不成立,因为所有内容都是在运行时直接执行的,因此使用像 C 的
#define
这样的机制是绝对没有用的——因此,回答你的问题,只需使用普通函数即可。The point of macros in C is that they are expanded at compile time.
Therefore, using the macros does not have an impact on the speed of your code, which a function doing the same would have.
Therefore, an example of usage is:
The code above will be translated by the compiler directly into:
The whole point of it is that it is faster than defining a function, such as this one:
Because there is no overhead (in a function, you need to push arguments to the stack, etc).
In an interpreted language, like PHP, the above doesn't hold, as everything is executed directly during run-time, therefore using a mechanism like C's
#define
would be absolutely useless -- therefore, to answer your question, simply use ordinary functions instead.只是你的评论的一个想法
just a thought from your comments
我不相信 PHP 支持宏。如果你仔细想想,C 中的宏并不是花哨的常量,而是常量是世界上最无聊的 C 宏。 PHP只能通过define函数来理解无聊的形式。
也就是说,我不同意其他受访者关于其有效性的观点。它们有一个合理的理由:当您想使用内置函数来预处理某些内容时。例如:
如果您尝试将其编写为函数并调用它:
那么如果未定义该变量链的任何部分,您将收到 PHP 警告,并且扩展版本更难以阅读:
PHP 代码是已编译,但它是在运行时编译的。像这样的宏将由 PHP 解释器扩展,然后编译成 PHP 字节码。该字节码通常由 PHP 解释器或 APC 等附加组件缓存。
I don't believe PHP supports macros. If you think about it, macros in C aren't fancy constants, rather constants are the world's most boring C macros. PHP only understands the boring form through the define function.
That said, I disagree with the other respondents about their validity. There is a valid reason for them: when you want to use built-in functions to preprocess something. For example:
If you try to write that as a function and call it:
then you'll get a PHP warning if any part of that variable chain is not defined, and the expanded version is harder to read:
PHP code is compiled, but it is compiled at runtime. A macro like this would be expanded by the PHP interpreter and then compiled into PHP bytecode. This bytecode is often cached by the PHP interpreter or an add-on like APC.
从这个意义上说,PHP 不支持宏,我认为(确实非常有效)的论点是,这与 PHP 中的普通函数没有太大区别。
毕竟,在运行时解释语言中拥有这样的概念没有任何价值。
PHP doesn't support macros in this sense, the (really quite valid) argument I believe being that there's not much difference between this and a normal function in PHP.
After all, it's not like there's any value in having a concept like this in a run-time interpreted language.