在 PHP 中声明变量类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/390932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring Variable Types in PHP?
提问by Dan Rosenstark
I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP:
我试图让我的 Netbeans 使用 PHP 自动完成,我了解到这段代码在 PHP 中是有效的:
function blah(Bur $bur) {}
A couple of questions:
几个问题:
- Does this actually impose any limitson what type of variable I can pass to the blah method?
- If this is just to help the IDE, that's fine with me. How can I declare the type of a variable in PHP if I'm not in a function?
- 这实际上对我可以传递给 blah 方法的变量类型施加了任何限制吗?
- 如果这只是为了帮助 IDE,那对我来说没问题。如果我不在函数中,如何在 PHP 中声明变量的类型?
回答by JW.
This type-hinting only works for validating function arguments; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when "blah" is called, but $bur could be reassigned to a non-Bur value inside the function.
这种类型提示仅适用于验证函数参数;您不能声明 PHP 变量必须始终是某种类型。这意味着在您的示例中,当“blah”被调用时, $bur 必须是 Bur 类型,但 $bur 可以重新分配给函数内的非 Bur 值。
Type-hinting only works for class or interface names; you can't declare that an argument must be an integer, for example.
类型提示仅适用于类或接口名称;例如,您不能声明参数必须是整数。
One annoying aspect of PHP's type-hinting, which is different from Java's, is that NULL values aren't allowed. So if you want the option of passing NULL instead of an object, you must remove the type-hint and do something like this at the top of the function:
PHP 的类型提示与 Java 不同,其中一个令人讨厌的方面是不允许使用 NULL 值。因此,如果您想要传递 NULL 而不是对象的选项,则必须删除类型提示并在函数顶部执行以下操作:
assert('$bur === NULL || $bur instanceof Bur');
EDIT:This last paragraph doesn't apply since PHP 5.1; you can now use NULL as a default value, even with a type hint.
编辑:最后一段自 PHP 5.1 起不再适用;您现在可以使用 NULL 作为默认值,即使有类型提示。
EDIT:You can also install the SPL Type Handlingextension, which gives you wrapper types for strings, ints, floats, booleans, and enums.
编辑:您还可以安装SPL 类型处理扩展,它为您提供字符串、整数、浮点数、布尔值和枚举的包装类型。
EDIT:You can also use "array" since PHP 5.1, and "callable" since PHP 5.4.
编辑:您还可以使用自 PHP 5.1 起的“array”和自 PHP 5.4 起的“callable”。
EDIT:You can also use "string", "int", "float" and "bool" since PHP 7.0.
编辑:从 PHP 7.0 开始,您还可以使用“string”、“int”、“float”和“bool”。
回答by Jim OHalloran
- Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
- Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.
- 如果您传递的值不是该类型的值,则为函数参数指定数据类型将导致 PHP 抛出可捕获的致命错误。但请注意,您只能为类指定类型,而不能为字符串或整数等基元指定类型。
- 如果提供,大多数 IDE 可以从 PHPDoc 样式注释推断数据类型。例如
/**
* @var string
*/
public $variable = "Blah";
回答by J Cooper
It's called type hinting, added with PHP 5. It isn't quite what you may be expecting if you are coming from a language like Java. It does cause an error to be thrown if you don't pass in the expected type. You can't type-hint primitives, though (no int $bur).
它被称为类型提示,是在 PHP 5 中添加的。如果您来自 Java 之类的语言,这可能不是您所期望的。如果您没有传入预期的类型,它确实会导致抛出错误。但是,您不能键入提示原语(没有 int $bur)。
回答by Shad
#2 : (...) How can I declare the type of a variable in PHP if I'm not in a function?
#2 : (...) 如果我不在函数中,如何在 PHP 中声明变量的类型?
I recently heard about "settype()" and "gettype()" in PHP4 & 5
You can force the variable type anytime easily
我最近听说PHP4 & 5 中的“ settype()”和“ gettype()”
你可以随时轻松地强制变量类型
From PHP.net :
从 PHP.net :
bool settype ( mixed &$var , string $type )
bool settype(混合 &$var ,字符串 $type )
Parameters
参数
var : The variable being converted. type : Possibles values of type are:
var : 被转换的变量。type : type 的可能值是:
- "boolean" (or, since PHP 4.2.0, "bool")
- "integer" (or, since PHP 4.2.0, "int")
- "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double")
- "string"
- "array"
- "object"
- "null" (since PHP 4.2.0)
- “boolean”(或者,自 PHP 4.2.0 起,“bool”)
- “整数”(或者,自 PHP 4.2.0 起,“int”)
- “float”(自 PHP 4.2.0 起才有可能,对于旧版本使用已弃用的变体“double”)
- “细绳”
- “大批”
- “目的”
- “null”(自 PHP 4.2.0 起)
[ :D First visit, first comment...]
[ :D 第一次访问,第一个评论...]
回答by Jeremy Ruten
Does this actually impose any limits on what type of variable I can pass to the blah method?
这实际上对我可以传递给 blah 方法的变量类型施加了任何限制吗?
This is called type hinting. According to the PHP documentation that I just linked to, yes, it does impose limits on the argument type: "Failing to satisfy the type hint results in a catchable fatal error."
这称为类型提示。根据我刚刚链接到的 PHP 文档,是的,它确实对参数类型施加了限制:“未能满足类型提示会导致可捕获的致命错误。”
How can I declare the type of a variable in PHP if I'm not in a function?
如果我不在函数中,如何在 PHP 中声明变量的类型?
Read type juggling. You can't explicitly define a variable's type in PHP, its type is decided by the context it is used in.
阅读类型杂耍。你不能在 PHP 中显式定义变量的类型,它的类型由它使用的上下文决定。

