PHP 内部如何实现类型忽略

发布于 2024-12-24 19:21:23 字数 307 浏览 3 评论 0原文

PHP 中如何实现类型忽略?例如,我可以编写以下函数:

function min($n, $m){
   if ($n<$m) return $n;
   return $m;
}

然后,我可以将该函数与整数、实数甚至字符串无关地使用。我相信这是可能的,因为 PHP 传递指针而不是实际变量(或对变量的引用)。我说得对吗?有人可以给我指出一个关于 PHP 内部实现的好讲座或者一个关于如何制作的好解释吗?

我应该补充一点,我对 PHP 解释器的工作方式非常感兴趣。感谢到目前为止提供的答案。

How is type ignorance implemented in PHP? For example, I can write the function:

function min($n, $m){
   if ($n<$m) return $n;
   return $m;
}

Then, I could use that function indifferently with integer, real or even string. I believe this is possible because PHP passes pointers instead of real variables (or reference to variables). Am I right? Can someone point me out a good lecture about internal implementation of PHP or a good explanation of how thing are made?

I should add that I am much interested into the way PHP interpreter make it work. Thank for the answers that were provided until now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

阳光下慵懒的猫 2024-12-31 19:21:24

这是因为 PHP 是动态的。 PHP 中的事物是按值传递的。这就是所谓的鸭子打字。

http://en.wikipedia.org/wiki/Duck_typing#In_PHP

Its because PHP is dynamic. Things are passed by value in PHP. Its called duck typing.

http://en.wikipedia.org/wiki/Duck_typing#In_PHP

荒人说梦 2024-12-31 19:21:24

默认情况下,PHP 按值传递变量,而不是按引用传递变量。通过将 function min($n,$m) 更改为 function min(&$n, &$m) 可以通过引用传递。有关传递引用的更多信息,请查看 PHP 手册上的此链接

这也可能对您有帮助: http://php.net/manual /en/language.variables.scope.php

By default, PHP passes variables by value, not by reference. It is possible to pass by reference by changing function min($n,$m) to function min(&$n, &$m). For more on pass-by-reference, check out this link on the PHP manual

This also may be helpful for you: http://php.net/manual/en/language.variables.scope.php

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