为什么是“传统"?PHP 中不允许类型提示? [英] Why is "traditional" type-hinting not allowed in PHP?

查看:33
本文介绍了为什么是“传统"?PHP 中不允许类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚发现 type-hinting 在 PHP 中是允许的,但是不适用于整数、字符串、布尔值或浮点数.

Just discovered that type-hinting is allowed in PHP, but not for ints, strings, bools, or floats.

为什么 PHP 不允许对整数、字符串等类型进行类型提示?

Why does PHP not allow type hinting for types like integer, strings, ... ?

推荐答案

PHP 的松散类型,其中您的原始"类型自动type-juggled 基于使用它们的上下文.类型提示不会真正改变这一点,因为字符串可以用作 int,反之亦然.类型提示只会对复杂类型(如数组和对象)真正有用,这些类型不能作为整数、字符串或其他原语干净利落地处理.

PHP's loosely typed, where your "primitive" types are automatically type-juggled based on the context in which they're used. Type-hinting wouldn't really change that, since a string could be used as an int, or vice versa. Type-hinting would only really be helpful for complex types like arrays and objects, which can't be cleanly juggled as ints, strings, or other primitives.

换句话说,因为 PHP 没有特定类型的概念,所以你不能在某处要求 int 因为它不知道 int 到底是什么是.另一方面,对象是某种类型的,因为MyClass不能与MyOtherClass互换.

To put it another way, since PHP has no concept of specific types, you couldn't require an int somewhere because it doesn't know what an int really is. On the other hand, an object is of a certain type, since a MyClass is not interchangeable with a MyOtherClass.

仅供参考,当您尝试在此类类型之间进行转换时会发生以下情况(并非详尽列表):

Just for reference, here's what happens when you try to convert between such types (not an exhaustive list):

转换为对象(ref)
如果一个对象被转换为一个对象,它不会被修改.如果任何其他类型的值被转换为一个对象,则创建一个 stdClass 内置类的新实例.如果value 为 NULL,新实例将为空.数组转换为具有由键命名的属性和相应值的对象.对于任何其他值,名为 scalar 的成员变量将包含该值."

Converting to Object (ref)
"If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value."

对象为 int/float(参考)
未定义的行为

Object to int/float (ref)
undefined behavior

对象到布尔值(ref)
在 PHP5 中,始终为 TRUE

Object to boolean (ref)
in PHP5, always TRUE

对象到字符串(ref)
对象的__toString() 魔术方法 将被调用(如果适用).

Object to string (ref)
The object's __toString() magic method will be called, if applicable.

对象到数组(ref)
如果一个对象被转换为一个数组,结果是一个数组,其元素是对象的属性.键是成员变量名,有几个值得注意的例外:整数属性是不可访问的;私有变量的类名被添加到变量名;受保护的变量在变量名前有一个 '*'.这些前置值在两边都有空字节.这可能会导致一些意外行为."

Object to array (ref)
"If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour."

数组到 int/float(参考)
未定义的行为

Array to int/float (ref)
undefined behavior

数组到布尔值(ref)
如果数组为空(即没有元素),则计算结果为 FALSE -- 否则为 TRUE.

Array to boolean (ref)
If the array is empty (i.e., no elements), it evaluates to FALSE -- otherwise, TRUE.

数组到字符串(ref)
字符串数组";使用 print_r()var_dump() 打印数组的内容

Array to string (ref)
the string "Array"; use print_r() or var_dump() to print the contents of an array

对象数组(ref)
数组转换为具有由键命名的属性和相应值的对象."

Array to object (ref)
"Arrays convert to an object with properties named by keys, and corresponding values."

这篇关于为什么是“传统"?PHP 中不允许类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆