如何在 PHP 中定义带有参数的宏,就像在 C++ 中一样?

发布于 2024-10-10 01:08:23 字数 310 浏览 4 评论 0原文

目标是有这样的东西:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不知在何时 2024-10-17 01:08:23

C 中宏的要点是它们在编译时扩展。

因此,使用宏不会影响代码的速度,而执行相同操作的函数则会影响代码的速度。

因此,一个使用的例子是:

#define MYPRINTF(x) printf("MYPRINTF says : %s\n",x)
MYPRINTF("blah");

上面的代码将被编译器直接翻译成:

printf("MYPRINTF says : %s\n","blah");

其要点在于它比定义一个函数要快,比如这个:

void myprintf(char *x)
{
  printf("myprintf says : %s\n","blah");
}

因为没有开销(在函数中,您需要将参数推入堆栈等)。

在像 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:

#define MYPRINTF(x) printf("MYPRINTF says : %s\n",x)
MYPRINTF("blah");

The code above will be translated by the compiler directly into:

printf("MYPRINTF says : %s\n","blah");

The whole point of it is that it is faster than defining a function, such as this one:

void myprintf(char *x)
{
  printf("myprintf says : %s\n","blah");
}

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.

段念尘 2024-10-17 01:08:23

只是你的评论的一个想法

function withVeryLongWroteByAnIdiot() {
   ....
}

function xFunc() {
   return withVeryLongWroteByAnIdiot();
}

just a thought from your comments

function withVeryLongWroteByAnIdiot() {
   ....
}

function xFunc() {
   return withVeryLongWroteByAnIdiot();
}
爱,才寂寞 2024-10-17 01:08:23

我不相信 PHP 支持宏。如果你仔细想想,C 中的宏并不是花哨的常量,而是常量是世界上最无聊的 C 宏。 PHP只能通过define函数来理解无聊的形式。

也就是说,我不同意其他受访者关于其有效性的观点。它们有一个合理的理由:当您想使用内置函数来预处理某些内容时。例如:

macro default_if_empty_value(value, default) (empty(value) ? default : value)

如果您尝试将其编写为函数并调用它:

default_if_empty_value($this->that['something']->or[$other], 'Not here.');

那么如果未定义该变量链的任何部分,您将收到 PHP 警告,并且扩展版本更难以阅读:

empty($this->that['something']->or[$other]) ? 'Not here.' : $this->that['something']->or[$other];

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:

macro default_if_empty_value(value, default) (empty(value) ? default : value)

If you try to write that as a function and call it:

default_if_empty_value($this->that['something']->or[$other], 'Not here.');

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:

empty($this->that['something']->or[$other]) ? 'Not here.' : $this->that['something']->or[$other];

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.

朕就是辣么酷 2024-10-17 01:08:23

从这个意义上说,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文