什么是动态类型?
我听说过这个术语与 PHP 等脚本语言一起使用。它到底是什么意思?
I've heard this term used with scripting languages such as PHP. What exactly does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我听说过这个术语与 PHP 等脚本语言一起使用。它到底是什么意思?
I've heard this term used with scripting languages such as PHP. What exactly does it mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
动态类型是语言的决定性特征。一个简短的解释可能是:
例如,在 PHP 中,您可以编写
然后继续说
这里发生了什么?其一,编译器不会抱怨您试图将字符串乘以数字并拒绝编译程序(就像在 C、C++、C# 和 Java 等语言中会发生的情况)。它生成了将参数
$count
和2
转发到乘法运算符的代码,就像您所要求的那样并继续前进。现在编译程序后,动态类型在运行时生效。当乘法运算符开始查看其操作数时,它会检查每个操作数的当前类型(如果您愿意)。和以前一样,它是一个字符串和一个整数。但该运算符知道它只能将两个整数相乘(为了简单起见,让我们忽略浮点数),因此它必须以某种方式从字符串中生成一个整数值。所有动态类型语言都有规定此类转换如何工作的规则所有值类型对之间;在这种情况下,PHP 生成整数 5 来自字符串“5”。
您可能会遇到的动态类型的另一个方面称为鸭子类型;这仅适用于类类型的值(即不是基元)。简而言之,鸭子类型规定,当您编写时,
编译器不会尝试查看
$object
是否属于具有名为quack
且不带参数的方法的类型。相反,它会在运行时尝试查看$object
是否确实具有这样的方法;如果是,无论什么类型都会调用该方法我们手头的对象(可能是一只鸭子,也可能是一只狗,编译器会关心)。
脚注:
1 将字符串乘以整数就是动态类型的全部内容(从字符串生成整数,因为乘法需要一个整数);然而,这里也存在松散类型(允许编译乘法而无需证明两个操作数实际上都是整数)。
Dynamic typing is a definitive characteristic of a language. A short explanation might be:
For example, in PHP you can write
and then go on to say
What happened here? For one, the compiler did not complain that you are attempting to multiply a string by a number and refuse to compile the program (such as would happen in languages like C, C++, C# and Java). It produced code to forward the arguments
$count
and2
to the multiplication operator just like you asked and moved on.With the program now compiled, dynamic typing comes into effect at runtime. When the multiplication operator gets around to look at its operands, it checks to see what is the current, if you will, type of each one. As before, it's a string and an int. But the operator knows that it can only multiply two integers (let's ignore floats for simplicity), so it has to somehow produce an integer value from the string. All dynamically typed languages have rules that stipulate how such a conversion works between all pairs of value types; in this case, PHP produces the integer 5 from the string "5".
Another aspect of dynamic typing you might come across is called duck typing; this only applies to values of class types (i.e. not primitives). In short, duck typing stipulates that when you write
the compiler will not attempt to see if
$object
is of a type that has a method namedquack
that takes no arguments. Rather, it will attempt at runtime to see if$object
actually has such a method; if it does, the method will be called regardless of what typeof object we have at hand (might be a duck, might be a dog for all the compiler cares).
Footnotes:
¹ Multiplying a string by an integer is what dynamic typing is all about (producing an integer from a string because the multiplication demands one); however, there is also loose typing at work here (allowing the multiplication to compile without being able to prove that both operands are actually ints).