PHP 内部如何实现类型忽略
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 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
默认情况下,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)
tofunction min(&$n, &$m)
. For more on pass-by-reference, check out this link on the PHP manualThis also may be helpful for you: http://php.net/manual/en/language.variables.scope.php